Does calling ~myclass() (destructor) deletes the object?

This topic is 1185 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I had a strange problem i was appending an array using delete/new

but after creation of new array i had to put the structs in it (texture loading- whenever i loaded texture that wasnt 256x256 i had to resize it -> ther i called mytexture.~TGLTexture(); //it seems that was the problem i had to write another function that wasnt named ~TGLTexture() with the exact code.

So my question is: does calling a destructor w/o delete, deletes the object? it seems that yes but i didint find anythig on the net about it

Edited by WiredCat

Share on other sites

You should never call the destructor directly. Always use delete.

Share on other sites

yeah but that wasnt the question ;] now i have a kill() procedure that is called in destructor. so i can safely call kill() without caling ~TGLTexture(), and everything works..

void kill()
{
{
if (pdata != NULL) delete [] pdata;
glDeleteTextures(1, &texture);
}
}

~TGLTexture()
{
kill();
}


so again the same question does calling ~TGLTexture() deletes the object? and why :P

Share on other sites

The answer to your question is no. It is called by delete somewhere in the stack but not the other way around. Seeing as the destructor should be calling delete for any allocations the object made, you should probably just add a resize method and modularize the functionality of the destructor so both can access necessary parts.

Share on other sites
	if (pdata != NULL) delete [] pdata;



If you do that, do this:

if (pdata != NULL)
{
delete[] pdata;
pdata = NULL;
}

In other words, make sure that your object is in a valid state in case someone calls kill twice, or kill once and delete once. Edited by Nypyren

Share on other sites
No, no, no. Unless an object is allocated with placement new, you must never call its destructor manually. See also Rule of Three. If a move constructor isn't appropriate for your needs, then use an init/cleanup methods.

EDIT:
Whoa, ninja'd x7. Edited by fastcall22

Share on other sites

Without knowing the error you had, and the definition of your destructor (ie. access modifiers and signature as well as body) it is hard to say why you needed to do anything beyond call the destructor.

Also I see your pointer is checked for NULL. Just initialize it as nullptr and call delete without needing to worry about it. Just set it to nullptr after the delete like suggested for NULL by Nypyren.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 15
• 14
• 46
• Forum Statistics

• Total Topics
634067
• Total Posts
3015325
×