Segment Query issue with Chipmunk Pro 6

Official forum for the Chipmunk2D Physics Library.
RodMath
Posts: 17
Joined: Sun May 09, 2010 2:48 pm
Contact:

Segment Query issue with Chipmunk Pro 6

Post by RodMath »

The following worked with Objective Chipmunk 5.3.4, and works in the simulator with v6.0.0, but gives an EXC_BAD_ACCESS error when I run it on the device:

Code: Select all

ChipmunkSegmentQueryInfo *info  = [mySpace segmentQueryFirstFrom:currPoint to:currTouch layers:CP_ALL_LAYERS group:CP_NO_GROUP];
I've checked that mySpace is valid, and both currPoint and currTouch are valid points so I'm at a bit of a loss as to what's going on.

Any ideas?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Segment Query issue with Chipmunk Pro 6

Post by slembcke »

Hey, sorry I was gone for the weekend.

I'm not sure how to reproduce the crash. Could you give me a more specific stack trace or some more information on how to replicate it?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
RodMath
Posts: 17
Joined: Sun May 09, 2010 2:48 pm
Contact:

Re: Segment Query issue with Chipmunk Pro 6

Post by RodMath »

No worries - I'm away from my computer for the next few days but will post back some more info when I get the chance.

As to how to replicate it, theoretically just create a space with a bunch of shapes in different layers and groups (that may not even be necessary) and do a segment query between two points. The query then works on the simulator but not on the device.

I know that sounds very basic, but that's effectively all I'm doing. Which has just made me wonder if perhaps my segment query is starting inside a shape, which is undefined, and the simulator handles the undefined silently while the device crashes out.... hmmm

As I say, when I get the chance I'll check that out and post back here.

Thanks
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Segment Query issue with Chipmunk Pro 6

Post by slembcke »

Hmm. That sounds a lot like a memory bug. It's possible that you might be over-releasing an object that is still added to the space or I might have forgotten a retain call somewhere. I'll look into it more on my end. See if I can get the unit tests to run under Valgrind.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
RodMath
Posts: 17
Joined: Sun May 09, 2010 2:48 pm
Contact:

Re: Segment Query issue with Chipmunk Pro 6

Post by RodMath »

Fortunately plans changed so I was able to look at this tonight. I decided the easiest would be to just try out the segment queries using the SimpleObjectiveChipmunk project and fortunately (?!) I was able to recreate the problem.

So, I changed the ViewController update function to be:

Code: Select all

- (void)update {
	// Step (simulate) the space based on the time since the last update.
	cpFloat dt = displayLink.duration*displayLink.frameInterval;
	[space step:dt];
	
	// Update the button.
	// This sets the position and rotation of the button to match the rigid body.
	[fallingButton updatePosition];
    

    ChipmunkSegmentQueryInfo *info = [space segmentQueryFirstFrom:cpv(10.f, 100.f)
                                                               to:cpv(310.f, 100.f) 
                                                           layers:CP_ALL_LAYERS
                                                            group:CP_NO_GROUP];
    if (info.t<1.f)
        NSLog(@"Line crossed");
}
And then I ran it using the iPhone 4.3 Simulator and it works fine, when the button crosses the line it prints line crossed. But, when I then run it on my device (iPhone 4.3.2), when the button crosses the line it crashes.

So I guess either I'm misunderstanding how to use segment queries or else there's a bug?


As an aside, I wish I'd looked at the SimpleObjectiveChipmunk project earlier - it's brilliant! I have been using obj chipmunk for over a year and yet it still taught me loads! The downside being my new found knowledge is going to make me re-engineer loads of stuff, but in a good, simplifying sort of a way.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Segment Query issue with Chipmunk Pro 6

Post by slembcke »

Hmm. That should give me something to work from. Thanks.

I guess I should be running the tests on the iPhone too. :-\
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Segment Query issue with Chipmunk Pro 6

Post by slembcke »

Frustrating... I was able to reproduce the issue with Objective-Chipmunk compiled in release mode but not in debug. I also can't get it to run on the phone with guard malloc. It fails when performing the dynamic linking which makes me suspect they simply took away the ability to use it on the device...

:evil:

Still working on it though.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Segment Query issue with Chipmunk Pro 6

Post by slembcke »

Ok, I found the bug... sort of. No idea what to do about it though.

It's actually not an Objective-Chipmunk bug, but a Chipmunk bug. It's crashing in this function:

Code: Select all

static void
SubtreeSegmentQuery(Node *subtree, void *obj, cpVect a, cpVect b, cpSpatialIndexSegmentQueryFunc func, void *data)
{
	if(cpBBIntersectsSegment(subtree->bb, a, b)){
		if(NodeIsLeaf(subtree)){
			func(obj, subtree->obj, data); // crashes when it tries to call this function pointer
		} else {
			SubtreeSegmentQuery(subtree->a, obj, a, b, func, data);
			SubtreeSegmentQuery(subtree->b, obj, a, b, func, data);
		}
	}
}
I could only get it to crash when compiled as release mode when on the device, so the debugger was mostly useless. I put a printf statement on the line before the func() call, and it printed out the correct address of the function, and all of the arguments were valid. I also put a printf() as the first line of the callback that func() calls, but it never reached it. The mystery deepens.

So I start disassembling the function and stepping through it to find this when it calls func():

Code: Select all

0x00008c44 <SubtreeSegmentQuery+444>:	ldr	r12, [sp, #16] // The register holding the function pointer is loaded here
0x00008c48 <SubtreeSegmentQuery+448>:	sub	sp, r7, #24	; 0x18
0x00008c4c <SubtreeSegmentQuery+452>:	pop	{r8, r10, r11}
0x00008c50 <SubtreeSegmentQuery+456>:	pop	{r4, r5, r6, r7, lr}
0x00008c54 <SubtreeSegmentQuery+460>:	bx	r12 // and called here
When SubtreeSegmentQuery() is first entered, the location of 'func' on the stack is indeed at [sp, #16], but by the time it calls it there have been a number of push/pops and it's now at [sp, #40]. It loads a garbage from the stack pointer into r12 instead and attemps to jump to it. Crash.

I want to say that this is a compiler bug in GCC, but Clang seems to crash in exactly the same way... I'm checking out it's assembly out put right now. :( What a pain.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Segment Query issue with Chipmunk Pro 6

Post by slembcke »

Aha. It turns out Clang doesn't seem to crash there. I brought Objective-Chipmunk in as a sub-project of SimpleObjectiveChipmunk. I set the parent project to use Clang, but not the Objective-Chipmunk one. So at least in the meanwhile that seems like a way to work around the bug.

I'm still trying to make a minimum replication project to try and confirm if it's a compiler bug though. Not that anybody will bother fixing something as ancient as GCC 4.2 if it is.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
RodMath
Posts: 17
Joined: Sun May 09, 2010 2:48 pm
Contact:

Re: Segment Query issue with Chipmunk Pro 6

Post by RodMath »

:shock: A compiler bug?!! Well if nothing else this definitely confirms I would never have found the problem.

I don't really know anything about compilers - other than that XCode uses GCC - are you saying I'd need to use Clang to compile my project, or can it be magically done in the ObjChipmunk I bring in?

Hmm, either way, seems like I better start googling Clang!

Thanks for your help
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests