destructor is not getting called, why??

would you please tell me why the destructors for the objects that pJ and pK are pointing were not called? I am sure there is a very simple answer for that.. but I just can't get it. help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
using namespace std;

class cFirst {
	public:
		cFirst(){ cout << "cFirstConstr@ " << this << endl;}
		~cFirst(){cout << "cFirstDestr@ " << this << endl;}
	protected:
		int x;
};

class cSecond: public cFirst {
	public:
		cSecond(){cout << "cSecondConstr@ " << this << endl;}
		~cSecond(){cout << "cSecondDestr@ " << this << endl;}
	private:
		int y;
};

int main(){

	cout << "pJ: " ;
	cFirst * pJ = new cFirst;
	cout << endl;
	
	cout << "pK: " ;
	cSecond * pK = new cSecond;
	cout << endl;
	
	cout << "L: " ;
	cFirst L;
	cout << endl;
	
	cout << "M :" ;
	cSecond M;
	cout << endl;
	
	cout << "O :" ;
	cFirst O;
	cFirst * pN = &O;
	cout << endl;
	
return 0;
}

/*
OUTPUT:

pJ: cFirstConstr@ 0x3e2528

pK: cFirstConstr@ 0x3e2538
cSecondConstr@ 0x3e2538

L: cFirstConstr@ 0x22ff10

M :cFirstConstr@ 0x22ff08
cSecondConstr@ 0x22ff08

O :cFirstConstr@ 0x22ff04

cFirstDestr@ 0x22ff04	--> O
cSecondDestr@ 0x22ff08	--> M
cFirstDestr@ 0x22ff08	--> M
cFirstDestr@ 0x22ff10	--> L

*/
Include delete pJ and delete pK at the end of your code. Read the documentation for keywords new and delete. The memory created using the operator new must be explicitly deallocated. Since the objects *pJ and *pK are never destroyed by the code, the desctructors are never called.
There are two reasons that your destructors aren't being called, one is as kishor8dm pointed out that you are using the operator "new" and because of that the "delete" command must be called explicitly.

The other is because the objects are inside of your main() function and as such their scope is the same as the program itself. If these were part of a function (and the "new" operator was not being used) then their destructors would be called at the end of that functions life.
The other is because the objects are inside of your main() function and as such their scope is the same as the program itself.
Then the destructors should be called at the end of the program.
@ ne555: I don't get it. The destructors are called at the end of the program. What do you mean?
Last edited on
well, I was waiting for my train, and all of a sudden, asked myself "why did you left the pointers behind?" :) I think I'm going crazy. you're right, they should be deleted. I could also call destructors explicitly I guess, which is really unnecessary.
BUT, I don't know about the scope issue, because when the objects are in the stack, and the program is over, everything is cleared up. that's truly my weak pointer/free store knowledge..
Thanks a lot for your replies.
Last edited on
I thought I caught that in time. I have never called a destructor explicitly until now, I honestly didn't think you could. What bothers me is that I can still reference it even after the destructor is called.
@Computergeek01: sorry, I misread your post.

You shouldn't explicit call the destructor. When the object goes out of scope it will be called again. (exception: placement new)

What bothers me is that I can still reference it even after the destructor is called.
I suppose that that is undefined behaviour.
for those who were wondering; after adding to line 42/43, inside main();

1
2
delete pJ;
delete pK;


the output is now;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
pJ: cFirstConstr@ 0x1fd6010

pK: cFirstConstr@ 0x1fd6030
cSecondConstr@ 0x1fd6030

L: cFirstConstr@ 0x7fff90f12590

M :cFirstConstr@ 0x7fff90f12580
cSecondConstr@ 0x7fff90f12580

O :cFirstConstr@ 0x7fff90f12570

cFirstDestr@ 0x1fd6010		--> object that pJ was pointing is killed
cSecondDestr@ 0x1fd6030		--> the one pK was pointing is killed (derived part)
cFirstDestr@ 0x1fd6030		--> base part of the same above (pK) is killed
cFirstDestr@ 0x7fff90f12570	--> O is gone
cSecondDestr@ 0x7fff90f12580	--> derived part of M is gone
cFirstDestr@ 0x7fff90f12580	--> base part of M is gone
cFirstDestr@ 0x7fff90f12590	--> L is gone

*/


which means everything is okay now.
Last edited on
Topic archived. No new replies allowed.