Freeing Memory Confusions

Official forum for the Chipmunk2D Physics Library.
lucas
Posts: 54
Joined: Wed Sep 26, 2007 2:34 am
Contact:

Freeing Memory Confusions

Post by lucas »

Hi,
I'm kind of confused as to what I need to free.
Does cpSpaceFree free all bodies, shapes, joints, etc. in the space?
Does cpBodyFree free the body's shape and/or vice versa?
Right now I'm freeing my object's SDL Surface, body, shape, the object itself and then removing it from my vector of objects. Is that correct?

Thanks!
Tangame - a tangram puzzle game with physics.
http://code.google.com/p/tangame/
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Freeing Memory Confusions

Post by slembcke »

You have to free everything that you allocate individually. If you make it using a cp*New(), you have to cp*Free() it.

Though there is a function cpSpaceFreeChildren(), that will free any body, shape, or joint that has been added to the space.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
lucas
Posts: 54
Joined: Wed Sep 26, 2007 2:34 am
Contact:

Re: Freeing Memory Confusions

Post by lucas »

This is still causing me grief, specifically Segmentation Fault troubles. After tracking it with GDB, it originates in cpSpaceStep, so I presume I am freeing my memory incorrectly. This is what I'm doing:

Code: Select all

for (int i = 0; i < objects.size(); i++) {
	SDL_FreeSurface(objects[i]->sprite);
	cpBodyFree(objects[i]->body);
	cpShapeFree(objects[i]->shape);
	delete objects[i];
	objects[i] = NULL;
	objects.erase(objects.begin()+i);
	i--; //this fixes the loop after erasing an element
}
A note of interest, I can still access the surface, body, shape and object until I set the parent pointer to null. For example, printing the value of objects->body->p still gives me the vector. I'm not sure if this is bad or it just means that my OS hasn't allocated anything to that memory yet.

Thanks,
Lucas
Tangame - a tangram puzzle game with physics.
http://code.google.com/p/tangame/
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Freeing Memory Confusions

Post by slembcke »

It looks like you are freeing the objects correctly, but are you removing them from the space? Where do you do that?

Also FYI, deallocating a chunk of memory does nothing special to it. It just marks it as available to be used by something else in your process. That memory is still likely owned by your process, so it won't crash when you access it. It also will contain whatever it did when it was deallocated until it either gets reaped by the OS or reallocated in your application.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
supertommy
Posts: 56
Joined: Tue Sep 11, 2007 2:30 pm
Contact:

Re: Freeing Memory Confusions

Post by supertommy »

The clever 'i--' at the end of the for loop does not do what you want. Since this loop will keep running until 'i' equals the original size of 'objects', you will walk beyond the end of the array. There really is no point in erasing all the elements, so you should just delete the two lines at the end of the for-loop.

Edit: I suppose, if you're going to re-use the list, you may actually want to remove all the elements. In that case, you should use 'objects.clear()' after the for-loop is finished. Also, the line with 'objects = NULL;' is not necessary, and can be removed if you want to.

Memory that has been free'd is not automatically set to 0 or anything, so finding the old values in the deleted memory is no surprise. That doesn't mean it's ok to use those values, though. If you keep the print statement which prints the deleted objects->body->p in your code, and then run it through Valgrind (http://en.wikipedia.org/wiki/Valgrind), it will tell you that you've got a problem.
lucas
Posts: 54
Joined: Wed Sep 26, 2007 2:34 am
Contact:

Re: Freeing Memory Confusions

Post by lucas »

Ah, foolish me, I didn't remove it from the space.

Also, thanks for clearing up what happens to the memory, I thought that might be the case, I just wasn't quite sure.
Tangame - a tangram puzzle game with physics.
http://code.google.com/p/tangame/
dc443
Posts: 20
Joined: Sun Dec 02, 2007 3:54 am
Contact:

Re: Freeing Memory Confusions

Post by dc443 »

I've got a very related question, so I figured i'd hijack this thread. ;)

I'm trying to delete a body, but have not kept track of the cpShapes, and cpBody does not contain any sort of data that points to its shapes. Since cpSpaceFreeChildren() will take care of all of that at the very end, can i just get away with simply freeing the body and removing the body from the space for the time being? Otherwise I would have to iterate through all of the shapes in the space by using space->activeShapes and then making a collection/array of the shapes that match the body that I'm trying to delete...

edit: well it looks like if I don't at least remove the shape from the space it still interacts with the objects in the space somehow. interesting.

edit: Also, i have no idea how to iterate through the shapes while passing some parameters at the same time. I guess i won't be adding the deleting functionality.
Last edited by dc443 on Wed Dec 12, 2007 3:41 pm, edited 1 time in total.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Freeing Memory Confusions

Post by slembcke »

Freeing a body that still have shapes pointing to it in an active space would be a very bad thing. You shouldn't be treating the physics objects as containers. Maybe it should be considered a design flaw in Chipmunk, but currently you are responsible for handling the memory management and relationship between the objects that you create. I prefer this way over using some sort of factory methods used on the space as it allows you to create a complete physics model separate from any given space complete with shapes, bodies and joints and then add it to a space when you are ready.

It was a simplification of the implementation for bodies to not know what joints and shapes they are attached to. I do need to do this eventually in order to implement body sleeping.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
dc443
Posts: 20
Joined: Sun Dec 02, 2007 3:54 am
Contact:

Re: Freeing Memory Confusions

Post by dc443 »

Alright, so currently, how might I go about removing something from the simulation? Say I want the user to click on an object with the mouse to delete it.

Edit: Hm. I guess I should just make a class/struct that contains a collection of shapes and the body, and keep track of that myself? Would that be the most straightforward way to implement this?
lucas
Posts: 54
Joined: Wed Sep 26, 2007 2:34 am
Contact:

Re: Freeing Memory Confusions

Post by lucas »

dc443 wrote:Hm. I guess I should just make a class/struct that contains a collection of shapes and the body, and keep track of that myself? Would that be the most straightforward way to implement this?
That is what I do, and it makes things nice and simple.
Tangame - a tangram puzzle game with physics.
http://code.google.com/p/tangame/
Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests