Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Deleting a vector of Shape pointers  (Read 5024 times)

0 Members and 3 Guests are viewing this topic.

hayer

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
Deleting a vector of Shape pointers
« on: June 08, 2013, 04:18:18 am »
I have a std::vector<sf::Shape*> that I clear at the end of my main loop.
In the main loop a unknown number of shapes get created and drawn( ConvexShape, CircleShape, etc).

The problem is; how do I clear the vector of shapes that has been drawn and is no longer needed?
The way I'm doing it now;

Code: [Select]
while(!this->m_shapes.empty())
{
delete this->m_shapes.back();
this->m_shapes.pop_back();
}

but this gives me a unhandled exception and brings up dbgheap.c

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: Deleting a vector of Shape pointers
« Reply #1 on: June 08, 2013, 04:47:45 am »
I use the same exactly structure when exiting my game to clean up my vectors of objects and items and have never encountered this problem.  Are you trying to access this vector again after it's emptied but before you put more shapes into it?  Can you recreate this and provide a minimal code example?

hayer

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
Re: Deleting a vector of Shape pointers
« Reply #2 on: June 08, 2013, 04:59:03 am »
Code: [Select]
int main(int argc, char *argv[])
{
sf::CircleShape myShape;
return 0;
}

gives me

Code: [Select]
Run-Time Check Failure #2 - Stack around the variable 'myShape' was corrupted.

And no, I'm not accessing them after deletion. I create a shape, add it to the vector, and pass it to draw. Later I call display and after that I clear the vector. Then I start over again.

massive_potato

  • Newbie
  • *
  • Posts: 37
    • View Profile
Re: Deleting a vector of Shape pointers
« Reply #3 on: June 08, 2013, 06:09:27 am »
Are you allocating new sf::Shapes with new, or are you getting their addresses with the & operator. If you're trying to delete an object that wasn't dynamically allocated (like in the code below), you'll get an error. Hope this helps!

int main()
{
    int c = 0;
    int * p = &c;
    delete p; // Not a valid deletion, since p wasn't dynamically allocated (it contains the address of a static variable)
    return 0;
}

int main()
{
    int * p = new int(0);
    delete p; // Valid, since p was dynamically allocated
    return 0;
}
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Deleting a vector of Shape pointers
« Reply #4 on: June 08, 2013, 09:01:58 am »
Don't mix debug and release configurations, as stated in the tutorial.
Laurent Gomila - SFML developer

hayer

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
Re: Deleting a vector of Shape pointers
« Reply #5 on: June 08, 2013, 02:16:07 pm »
Don't mix debug and release configurations, as stated in the tutorial.

Okey, running in debug, as earlier, same problem - any specific settings I should check for?


Edit1:
Running in release I get this message;
Code: [Select]
Unhandled exception at 0x00042569 in OPEngine.exe: Stack cookie instrumentation code detected a stack-based buffer overrun.

Edit2:
Okey, seems to be something more wrong in my release config. I get
Code: [Select]
Unhandled exception at 0x00D82569 in OPEngine.exe: Stack cookie instrumentation code detected a stack-based buffer overrun.
with the shapes removed... God I need to clean this mess up -- and now it seems like SFML is forcing me to  ::)

Edit3:
Soo now everything seems a bit nicer. I have set all the projects to compile as MDd in debug and as MD in release. They all use "Use Unicode Character Set" as character set in both debug and release. What more should I check?

Currently I'm using dynamic libs from SFML - I can't see any problem with that?




Final edit:
Got it working. Changed to Multi-byte character set for all projects - as that was what SFML used in the build project. Reverted to stable source-code(Might not have made any difference but did it for simplicities sake). Built with stable, linked with solution - Tadaadaa  ;D
« Last Edit: June 08, 2013, 03:19:33 pm by hayer »