Skip to main content
gamedev.net
🔒 Locked

Release and pointer = NULL question

Started by Talib 0 replies 200+ views
Original Post
Talib
Talib
Hi I understand the whole idea behind Release() and all memory leak issues. Most people suggest setting your pointers to NULL such as: if (mypointer != NULL) { mypointer->Release(); mypointer = NULL; } I was just wondering in my own classes if I could do the following: MyClass::Release() { delete this; this = NULL; //I know this doesn't work. } I get the following error: c:\Documents and Settings\Default\My Documents\Visual Studio Projects\Scuttle\SpriteSplash.cpp(91): error C2102: '&' requires l-value Basically is there a way of the Release() of a class automatically setting itself equal to NULL? And will the destructor and all the code work correctly? Thanks Talib
Try, try and fucking try again.
Promit
Promit
delete this; is legitimate to do. But be extremely careful that nobody is using your class instance, because they will be neatly crashed afterwards if they are. (This means being very clear about who's responsibility it is to AddRef and Release things.)

"this = NULL;" is entirely meaningless, because this simply provides you with information about yourself. If you delete yourself, you no longer exist. If your instance is deleted, there's no way any of your functions can be re-entered, so nobody can ever check if( this == NULL ) anyway.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.