Jump to content

  • Log In with Google      Sign In   
  • Create Account


Simple question regarding deleting allocated memory


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 johnmarinelli   Members   -  Reputation: 304

Like
0Likes
Like

Posted 17 January 2013 - 06:44 PM

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!


Edited by johnmarinelli, 17 January 2013 - 06:49 PM.


Sponsor:

#2 RulerOfNothing   Members   -  Reputation: 1143

Like
0Likes
Like

Posted 17 January 2013 - 06:54 PM

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)

Edited by RulerOfNothing, 17 January 2013 - 06:55 PM.


#3 ApochPiQ   Moderators   -  Reputation: 14104

Like
0Likes
Like

Posted 17 January 2013 - 06:55 PM

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
}


#4 johnmarinelli   Members   -  Reputation: 304

Like
0Likes
Like

Posted 17 January 2013 - 07:01 PM

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.



#5 RulerOfNothing   Members   -  Reputation: 1143

Like
0Likes
Like

Posted 17 January 2013 - 07:27 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS