I have a code that need to remove bodies. It seems to work correctly but when i use valgrind, it tells me that some code try to access the removed bodies.
==10874== Invalid read of size 4
==10874== at 0x4040B0D: cpBodyIsSleeping (cpBody.h:141)
==10874== by 0x4041795: cpSpaceStep (cpSpaceStep.c:353)
==10874== by 0x8048D3F: main (test_cp.c:81)
==10874== Address 0x4219bcc is 188 bytes inside a block of size 204 free'd
==10874== at 0x4024A66: free (vg_replace_malloc.c:366)
==10874== by 0x40382BB: cpBodyFree (cpBody.c:106)
==10874== by 0x8048AB7: body_free (test_cp.c:41)
==10874== by 0x8048D9B: main (test_cp.c:90)
==10874==
==10874== Invalid write of size 4
==10874== at 0x403C671: unthreadHelper (cpArbiter.c:55)
==10874== by 0x403C6C9: cpArbiterUnthread (cpArbiter.c:68)
==10874== by 0x40417A7: cpSpaceStep (cpSpaceStep.c:354)
==10874== by 0x8048D3F: main (test_cp.c:81)
==10874== Address 0x4219bc4 is 180 bytes inside a block of size 204 free'd
==10874== at 0x4024A66: free (vg_replace_malloc.c:366)
==10874== by 0x40382BB: cpBodyFree (cpBody.c:106)
==10874== by 0x8048AB7: body_free (test_cp.c:41)
==10874== by 0x8048D9B: main (test_cp.c:90)
Hmm. I tried to test this pretty heavily with libGuardMalloc, but I might have missed a couple dangling pointers. I'll have to take another look at this.
Also, thank you, thank you, thank you for a simple test program! That makes it so much easier to find bugs.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Neat. So I've never actually tried Valgrind before. Apparently I should more often.
One of the warnings was because you were removing a shape from the callback for cpBodyEachShape(). I fixed cpBodyEachShape() to use a while loop so it's safe to remove them now. The second invalid read I think I understand what is happening, but not how it got to that state. The invalid write in unthreadHelper() is a result of the bad read.
I haven't committed anything yet, I'll have to take a closer look tomorrow probably.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Ok! So I modified it so that you can remove objects from a space during a cpBodyEach*() call. That was one of your warnings. The second was a dangling pointer bug that I fixed.
Thank you for the clear replication steps. That's always very helpful.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/