Help with allocation of bodies in cocos2d!

Official forum for the Chipmunk2D Physics Library.
Post Reply
PacBros64
Posts: 1
Joined: Fri May 02, 2014 9:15 am
Contact:

Help with allocation of bodies in cocos2d!

Post by PacBros64 »

Hello everyone! It's my first post but I've been lurking the forums for a long time! I'm developing a game on cocos2d 2.x with chipmunk physics and I've run into a strange issue that I don't know how to solve. Basically, upon initializing the layer acting as the gameplay screen, the bodies/shapes are added to the space. Everything goes well except that when I pause the game and select to go back to the world map, and re-enter the level, I get a bad_exc_access error. I traced the error to the layer's dealloc method where all the bodies are freed. Basically when I go from the level to the world map and back to the level, the layer is then re-initialized and in theory, the bodies and shapes get initialized and added again. The problem is that the shapes and bodies are null for sons reason.

The program works normally if I comment out the code that frees the shapes and bodies in the dealloc method but then I get two instances of each shape and body. I can tell because when I turn on the debug view, the polygons are a darker color (dark gray for static ones and dark red for dynamic ones). Meaning I'll be leaking memory every time I exit and re-enter a level. Currently I only have one level so I can't know what happens if I exit one level and enter a different one.

Thanks guys for the help!

EDIT: Hi guys! I solved my problem! I had to actually free the space upon exiting the level as well. I didn't know I had to because the space is part of a singleton class. Anyways, I had to modify my code to make the singleton class free the space upon exiting, then upon entering the level, re-initializing the space. I hope this info helps others in the future!
Post Reply

Who is online

Users browsing this forum: No registered users and 28 guests