deleting an Object

int main()
{
class B {

};
class A {
public:
~A() {
delete el;
}
B* el;
};
B* b = new B;
A a;
a.el = b;
a.~A();

}
--------------------------------------------

I dont get simple deleting. I thaught if i alloc an object with new i can delete it but my machine always crashes when i try it.
when i have a pointer el in class A from class B i can not delete it and i have no clue whats the problem about it.
The destructor of the object is getting invoked twice;
once explicitly with a.~A(); and then again when block (main) is exited from.

Once a destructor is invoked for an object, the object no longer exists; the behavior is undefined if the destructor is invoked for an object whose lifetime has ended.

[ Example: If the destructor for an automatic object is explicitly invoked, and the block is subsequently left in a manner that would ordinarily invoke implicit destruction of the object, the behavior is undefined. — end example ]
http://eel.is/c++draft/class.dtor#16
Numer1: does not work
class B {};
class A {
public:
~A() {
delete el;
}
B* el;
};
B* b = new B;
A a;
a.el = b;
delete a.el;

Number2: works
class C{};
class D {
public:
C* c;
};
C* ce=new C;
D d;
d.c = ce;
delete d.c;

------------------------------------

for me its the same code am i blind?
you call the destructor of A twice.
once manually, which is a no-no, and once automatically, when it is destroyed by going out of scope.

remove the call to a's destructor, and it should work.
but you probably want to be more careful. when you delete a pointer, set it to null right away so its safe to delete again if you do so accidentally.

okay thank you :)
no, they are not the same.
a deletes el manually with a delete statement..
then it does it again in the destructor.
but you didn't null the pointer, so it tries to delete memory it does not own. Crash!
eg the new statement gives you ownership of block 1234 in memory.
delete gives it back.
delete again tries to give back 1234 but you don't own it anymore, this is the problem. See the beavis and butthead candy-bar sales episode to understand why this does not work.

you have 2 problems. You don't understand classes, and how the destructor works, and you don't understand pointers, and how delete works. And you are playing this off each other confusingly... you need to focus on one issue or the other (play with destructors a bit without pointers, try a print statement) and play with pointers (without any classes at all).
Last edited on
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2) What are you trying to achieve with the last line of your main() function? You never directly invoke a destructor like that; destructors are called automatically when an object is destroyed.
try these: (and check out the <> format button on the side for posting code)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
class B {

};
class A {
public:
~A() {
cout << "dtor for A\n";   //watch what this does. 
}
B* el;
};
B* b = new B;
A a;
a.el = b;
a.~A();

}


and

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
class B {

};
class A {
public:
~A() {
delete el;
el = 0;   //this should fix the problem.  But you still should not
}
B* el;
};
B* b = new B;
A a;
a.el = b;
a.~A();     //have this line.  You never want to manually call a dtor.  

}
Last edited on
Topic archived. No new replies allowed.