Memory leak(?)

Discuss any Chipmunk bugs here.

Memory leak(?)

Postby damir » Sat Feb 23, 2008 9:42 am

Hi. I've been creating and freeing objects in my application and noticed the memory usage is growing.
So I took MoonBuggy tutorial and modified it so it adds and removes a static shape from a static body every frame.
Here is the code:
Code: Select all
cpShape *seg = cpSegmentShapeNew(staticBody, cpv(1,0), cpv(-1,0), 0.0f);
cpSpaceAddStaticShape(space, seg);
cpSpaceRemoveStaticShape(space,seg);
cpShapeFree(seg);


I added this to moonBuggy_update function which makes it execute every frame. From what I understand, cpShapeFree should take care for cleaning all the data, but there is a memory leak since the process is taking up more memory every second. (Actually if you want to test it, wrap it in a for loop and do about 100 iterations for the memory growth to be faster).
I also tried using Delete function instead of Free and then freeing 'seg' variable on my own. Result was the same.
Am I doing something wrong? Is there a special Free functon for static shapes (I haven't found any in docs or chipmunk's code)?
This is important for me because I am adding and destroying objects and shapes extensively in my application.
damir
 
Posts: 2
Joined: Sat Feb 23, 2008 9:24 am

Re: Memory leak(?)

Postby Michael Buckley » Mon Feb 25, 2008 12:35 pm

I have to leave now, but I did a little testing. It doesn't seem to be the creation and deletion of objects that is causing the memory growth (the leaks tools says there are no leaks), but rather the adding and removing of objects. My current guess is that the insertion of the shape into the cpShapeHash calls a function which can retain the shape's handle more than once, but the removal function only releases the shape's handle once. This is just a guess though.
Michael Buckley
 
Posts: 46
Joined: Tue Aug 21, 2007 10:30 am

Re: Memory leak(?)

Postby slembcke » Mon Feb 25, 2008 1:41 pm

Ah, I bet I know what this is. Static shape hash... The hash rarely gets cleared so it never decrements the retain counts on the handles. Calling cpSpaceRehashStatic(), clears and rehashes. That should clear out any cells pointing to the handle and allow it to be released.

If you are adding and removing static shapes a lot, you probably want to rehash occasionally anyway. That will clear out the stale handles that will eventually start to slow down the broadphase check.
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: Memory leak(?)

Postby Michael Buckley » Mon Feb 25, 2008 3:14 pm

I should also point out that, in the context of my test code, I saw the exact same behavior with non-static shapes. The code simply creates a body, creates a space, and then loops through a set number of times, creates a new shape for the body, adds the shape to the space, removes it from the space, and frees the shape. Then, after the loop is complete, it sleeps for a while before exiting.

I don't know if the handles of non-static shapes would get released during the operation of a normal program.
Michael Buckley
 
Posts: 46
Joined: Tue Aug 21, 2007 10:30 am

Re: Memory leak(?)

Postby viblo » Mon Feb 25, 2008 4:22 pm

Michael Buckley wrote:I should also point out that, in the context of my test code, I saw the exact same behavior with non-static shapes. The code simply creates a body, creates a space, and then loops through a set number of times, creates a new shape for the body, adds the shape to the space, removes it from the space, and frees the shape. Then, after the loop is complete, it sleeps for a while before exiting.

I don't know if the handles of non-static shapes would get released during the operation of a normal program.


I can only reproduce the leak when I add static shapes to the space (without rehash), not non-static.

This code uses more and more memory
Code: Select all
init_pymunk()
space = Space()   
while True:   
    body = Body(1,1)
    shape = Segment(body, vec2d(0, 0), vec2d(0, 0), 0.0)
    space.add_static(shape)
    space.remove_static(shape)
    space.step(1.0/60)

This code doesn't
Code: Select all
init_pymunk()
space = Space()   
while True:   
    body = Body(1,1)
    shape = Segment(body, vec2d(0, 0), vec2d(0, 0), 0.0)
    space.add(shape)
    space.remove(shape)
    space.step(1.0/60)

This code doesn't
Code: Select all
init_pymunk()
space = Space()   
while True:   
    body = Body(1,1)
    shape = Segment(body, vec2d(0, 0), vec2d(0, 0), 0.0)
    space.add_static(shape)
    space.resize_static_hash()
    space.remove_static(shape)
    space.step(1.0/60)

(everything from python with pymunk but I think you can read it :) )
http://www.pymunk.org - A python library built on top of Chipmunk to let you easily get cool 2d physics in your python game/app
viblo
 
Posts: 206
Joined: Tue Aug 21, 2007 3:12 pm

Re: Memory leak(?)

Postby Michael Buckley » Mon Feb 25, 2008 8:35 pm

Perhaps I should clarify. I don't know if I saw increasing memory usage. My script ran for a set number of loops before I checked the memory usage. What I did see is that adding a shape and removing it resulted in an increased memory usage over not doing so. Also, my code did not call cpSpaceStep.
Michael Buckley
 
Posts: 46
Joined: Tue Aug 21, 2007 10:30 am

Re: Memory leak(?)

Postby damir » Tue Feb 26, 2008 3:26 am

OK rehash function worked for me. Thanks!
No problems with non-static shapes.
damir
 
Posts: 2
Joined: Sat Feb 23, 2008 9:24 am


Return to Bugs

Who is online

Users browsing this forum: No registered users and 0 guests

cron