Jump to content
  • Advertisement
Sign in to follow this  
KKTHXBYE

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 (texloaded)
{
	if (pdata != NULL) delete [] pdata;
	glDeleteTextures(1, &texture);
	texloaded = false;
}
}

~TGLTexture()
{
kill();
}

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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. sad.png Edited by fastcall22

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!