Simple question regarding deleting allocated memory

Started by
3 comments, last by RulerOfNothing 11 years, 2 months ago

Hi everyone,

So I have a class called c_Zombie. I also have a class called c_AIstates, which is an FSM for the zombie's AI.

In my zombie class, I create a pointer of type c_AIstate:

(pseudocode)


class c_Zombie
{
private:
c_AIstate *currentAIstate;

public:
...
}

it also has a pointer to the animation class as well. many of my entity classes contain pointers to their various "components".

my question is: why do I get errors when I delete my pointers in the Zombie destructor?


c_Zombie::~c_Zombie(void)
{
	delete currentAIstate;
	currentAIstate = NULL;

	delete onAnimate;
	onAnimate = NULL;
}

I'm sure that I should know the answer but I dont sad.png

also,

assuming that i actually SHOULDN'T be explicitly deleting the pointers in the Zombie's dctor, how does the memory get deleted? or should it not be deleted at all?

EDIT: okay, the program is working now - however, I know with certainty that the same issue arises if I delete an SDL_Surface* in the dstructor, and/or an onAnimate* in some other classes.

Thanks for your time!

Advertisement
The problem here appears to be that either currentAIstate was not constructed with new, or that you are sharing AI states between c_Zombie instances (so when you delete one c_Zombie's AI state, you are causing problems for other instances which were still using that AI state). Keep in mind that you should only use delete on objects constructed with new (so don't use delete on SDL_Surfaces)
What you do with a pointer in a destructor depends entirely on how you got that pointer.

The problem with raw pointers is they have no concept of ownership semantics - i.e. who is responsible for cleaning up the object. Smart pointers are the solution to that problem; I'd suggest studying up on them.

In the interim, the easy solution is to make sure you know who owns the object you're trying to delete. Also, remember that you only delete what you new, and only delete[] what you new[]. So:
void Frobnicate(SomeObject* p)
{
    p->DoStuff();
    delete p;
}

int main()
{
    SomeObject foo;
    Frobnicate(&foo);   // BAD! I didn't new foo, so I shouldn't delete it

    SomeObject* bar = new SomeObject;
    Frobnicate(bar);    // OK
    bar->MoreStuff();   // BAD! I own bar, but Frobnicate() deleted it out from under me
}

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Thanks guys, your responses helped a lot. I've heard of smart pointers, and plan to really look into them soon.

One more question:

If I don't delete onAnimate in an objects destructor, and that object gets deleted (say the player enters a building and all the objects from the previous gamestate are deleted), will a "memory leak" result? I understand the concept of a memory leak; however I can't say I know exactly how to tell when one has happened/is happening.

That depends on what onAnimate actually points to. Is it a function pointer or does it point to some sort of wrapper class? In the first case, no memory was actually allocated (and since you should only delete what you new, you shouldn't delete function pointers). In the second case, it will leak memory if it was allocated on the heap.

This topic is closed to new replies.

Advertisement