Jump to content
  • Advertisement
Sign in to follow this  
nano511

After destructor is called,

This topic is 2537 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

Everytime i 'kill' a monster i delete it. After its destructor is called i get this..

Unhandled exception at 0x68129397 in MyGame.exe: 0xC0000005:
Access violation reading location 0xfeeeff26.

Share this post


Link to post
Share on other sites
Advertisement
Do you call delete twice on the same pointer or something? It's just a guess because it's hard to know by just looking at that error message.

Share this post


Link to post
Share on other sites

hmm.. maybe? Is there a way to check that? like.. if( not deleted){ delete }


From what i khnow there is no way to do it. You cannot check if you have alreasy called delete on a pointer.

Generally the idea is: if someone allocates memory, that someone has to free it.

Share this post


Link to post
Share on other sites
No. There is no way to tell. That's the exciting challenge of C++. It's also why smart pointers were invented. Smart pointers mean you never have to explicitly call delete again.

Share this post


Link to post
Share on other sites

No. There is no way to tell. That's the exciting challenge of C++. It's also why smart pointers were invented. Smart pointers mean you never have to explicitly call delete again.


I agree. Get the hang of using shared_ptr and weak_ptr and you wont look back.

Share this post


Link to post
Share on other sites
I know from your previous posts that you store monster pointers in a vector. If you delete the monster from the vector you probably also want to remove the pointer from the vector. One way of doing that is to use std::vector::erase. It takes an iterator as argument but I'm not going to explain iterators now so I just give some code.
for(std::size_t i = 0; i < monsters.size(); i++ )
{
if(monsters->IsDead())
{
delete monsters;
monsters.erase(monsters.begin() + i); // this removes the pointer from the vector
}
}

Note that all monster after the deleted monster will move down one index in the vector and the size of the vector will be one smaller.

Share this post


Link to post
Share on other sites
Thanks for that Wooh! But that doesnt change the fact that after the destructor is finished, i get that error.

EDIT: Oh... nvm your suggestion did fix it!

Share this post


Link to post
Share on other sites
Just one last question for tonight:

When a monster dies i do this "player->points += 100". But since after the monster dies that will always be true, it keeps adding 100 each frame. How do i stop that?

Share this post


Link to post
Share on other sites
You mean IsDead() is always true? When the monster has been deleted you never call IsDead() on it again so that shouldn't cause any problems I think. I can't see your code so I just speculating.

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!