https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83414
Pablo M. S. Farias <pmsf at ufc dot br> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |RESOLVED Resolution|--- |INVALID --- Comment #2 from Pablo M. S. Farias <pmsf at ufc dot br> --- Indeed, you are right, and I am sorry for my silly programming mistake. I realized that my comment "The attached program ... never delete's the pointers received." is wrong: given the destructor of class C, of course "o.v" gets deleted when "o" goes out of scope in "main". However, I really wanted to say something else: that the pointers are printed before any of them gets deleted. This, however, turns out to be my "gotcha": the destructor of parameter "o" of function F1 actually deletes the array originally allocated at line C o (3); of "main". That is why, when the second allocation occurs in F2 p.node = new Node; we may get the same pointer again. :-( Just for the record, I found this out by running the following variation of the original program, which keeps track of constructor and destructor calls: ---------------------------------------------------------------------- #include <fstream> #include <iostream> #include <new> using std::cout; struct Node { double key; float value; }; struct Pack { double key; float value; Node *node; }; class C { public: Pack *v; C (int size) : v (new Pack [size]) { cout << "CONS with " << size << " and " << v << '\n'; } ~C () { cout << "DES with " << v << '\n'; delete[] v; } }; Pack F1 (C o) { Pack p; return p; } void F2 (Pack &p) { std::ofstream file ("any.txt"); file.close(); p.node = new Node; } int main () { cout << "Before creating o\n"; C o (3); cout << "After creating o\n"; Pack p = F1(o); cout << "After calling F1\n"; F2 (p); cout << "After calling F2\n"; std::cout << "o.v: " << o.v << '\n'; std::cout << "p.node: " << p.node << '\n'; } ---------------------------------------------------------------------- Here is a sample output showing that the destructor of C gets called at the end of F1: ---------------------------------------------------------------------- Before creating o CONS with 3 and 0x8fd7e18 After creating o DES with 0x8fd7e18 After calling F1 After calling F2 o.v: 0x8fd7e18 p.node: 0x8fd7e18 DES with 0x8fd7e18 ---------------------------------------------------------------------- In the end, my mistake was that the parameter of F1 should have been "C &o", not "C o". Again, sorry for the mistake.