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

Started by
29 comments, last by Zipster 8 years, 6 months ago

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

Advertisement

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

Manually calling the destructor just runs the destructor code, it doesn't free the memory for the object like delete would.

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

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.

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

No. Construction and destruction are two entirely separate things in C++.

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

There are plenty of reasons to call destructors directly. Placement newed objects for instance. In collections, etc.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.


	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.


	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.

if you do that... do this:

    delete[] pdata;
    pdata = NULL;
Hooray! A useless null check is gone.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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

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.

This topic is closed to new replies.

Advertisement