Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why this code doesn't crash ?

Status
Not open for further replies.

Dhafir

Programmer
Sep 22, 2002
28
0
0
NZ
Hi,
Car* myCar = new Car();
delete myCar;
mayCar->start();// assume class Car has a start() method

Why after deleting myCar I was still able to dereference it without problem?

Dhafir
 
My only guess is that the memory was only marked as being deleted but that space had not yet been used. For some freak of nature (or compiler) it worked. I would say never trust it but because you do it so fast after the delete, the memory is still good. Remember I can always say

CObject* pObject;
pObject->IsKindOf(RUNTIME_CLASS(MyObject));

Because I dont set pObject to null, it runs, compiles and crashes. In your case, the address was valid. Try AfxAssertValidAddres on the pointer (I think that is the name of it.. i know validaddress is part of it)

matt
 
Car* myCar = new Car();
delete myCar; //below you have mayCar, not myCar as here
mayCar->start();// Ion Filipski
1c.bmp


filipski@excite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top