[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 488: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 112: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/bbcode.php on line 112: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4762: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3897)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4764: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3897)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4765: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3897)
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4766: Cannot modify header information - headers already sent by (output started at [ROOT]/includes/functions.php:3897)
Chipmunk Physics • View topic - Freeing Memory Confusions

Freeing Memory Confusions

Official forum for the .

Freeing Memory Confusions

Postby lucas » Sat Dec 01, 2007 3:41 am

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/
lucas
 
Posts: 54
Joined: Wed Sep 26, 2007 2:34 am

Re: Freeing Memory Confusions

Postby slembcke » Sat Dec 01, 2007 6:37 pm

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/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: Freeing Memory Confusions

Postby lucas » Wed Dec 05, 2007 2:19 am

Tangame - a tangram puzzle game with physics.
http://code.google.com/p/tangame/
lucas
 
Posts: 54
Joined: Wed Sep 26, 2007 2:34 am

Re: Freeing Memory Confusions

Postby slembcke » Wed Dec 05, 2007 9:45 am

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/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: Freeing Memory Confusions

Postby supertommy » Wed Dec 05, 2007 9:55 am

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[i] = 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[i]->body->p in your code, and then run it through Valgrind (), it will tell you that you've got a problem.
supertommy
 
Posts: 56
Joined: Tue Sep 11, 2007 2:30 pm

Re: Freeing Memory Confusions

Postby lucas » Thu Dec 06, 2007 2:11 am

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/
lucas
 
Posts: 54
Joined: Wed Sep 26, 2007 2:34 am

Re: Freeing Memory Confusions

Postby dc443 » Wed Dec 12, 2007 3:14 pm

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.
dc443
 
Posts: 20
Joined: Sun Dec 02, 2007 3:54 am

Re: Freeing Memory Confusions

Postby slembcke » Wed Dec 12, 2007 3:41 pm

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/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: Freeing Memory Confusions

Postby dc443 » Wed Dec 12, 2007 3:44 pm

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?
dc443
 
Posts: 20
Joined: Sun Dec 02, 2007 3:54 am

Re: Freeing Memory Confusions

Postby lucas » Wed Dec 12, 2007 9:14 pm

Tangame - a tangram puzzle game with physics.
http://code.google.com/p/tangame/
lucas
 
Posts: 54
Joined: Wed Sep 26, 2007 2:34 am

Next

Return to Chipmunk2D Physics

Who is online

Users browsing this forum: No registered users and 1 guest