Box2D joints with Classes

Ok so I made a few classes to make things easier, but when I try to use a body from the class, xcode enters the debugger, and stops at one point. It normally doesn't do this, and goes strait to the game.

The code for box2D is here:
Code:
RectFactory rect(-270.0f, 240.0f, 200.0f, 200.0f, 1.0f, world);
   RectFactory groundRect(0.0f, -300.0f, 3000.0f, 600.0f, 0.0f, world);
   
   CircleFactory circle(270.0f, 240.0f, 100.0f, 1.0f, world);
   
   b2Body* rectBody = rect.body;
   b2Body* circleBody = circle.body;
   
   b2DistanceJointDef jointDef;
   jointDef.Initialize(rectBody, circleBody, rectBody->GetPosition(), circleBody->GetPosition());
   jointDef.collideConnected = true;
   
   b2DistanceJoint* joint = (b2DistanceJoint*)world->CreateJoint(&jointDef);


Rect/Circle Factories both have : b2Body* body in them, so rect/circle.body should work for both of them.

So then why wont this work, anyone know?
Are the destructors in your factories deleting or removing the bodies from the world or something like that? Because it looks like RectFactory and CircleFactory are allocated on the stack (sort of an odd use of the term "factory," btw, see http://en.wikipedia.org/wiki/Factory_method_pattern) which means the destructors will be called when the current stack frame pops and everything will blow up afterwards when the world tries to use those bodies.

If you're not deleting the bodies in the destructors, though, I don't see anything immediately. Can you explain what type of crash you're seeing, and maybe poke around in the debugger a bit to see what might be wrong (see if there are any NULL pointers or odd numbers in local variables, maybe)?
Well I'm not using a very complex class, just something simple to start me out. I have no defined detructor (just the default one of corse), but maybe that's the problem.

I tried it without the class and it worked fine. The funny thing is the debbuger just says there's an error at any random point in the file.
Hmm, can you post the code for CircleFactory and/or RectFactory? If something's going wrong when you put your body creation code into those but it works outside, that's probably what we should be looking at, because as far as I can see from this snippet, if those classes are doing what they should be, you shouldn't have a problem.

Sounds like a memory issue, from what you're saying (possibly unrelated to the physics) - often there's some sort of corruption happening somewhere (writing to an invalid pointer, off the end of an array, etc.) when you get random crashes at unpredictable places in your code.
Oh ok thatmakes sence. Il post them in a little bit, I'm on my iPod now, but once I get on my computer il post them.

Thanks for taking the time to help,
Kcaz
Oh ok thatmakes sence. Il post them in a little bit, I'm on my iPod now, but once I get on my computer il post them.

Thanks for taking the time to help,
Kcaz
Sorry about the 2 posts before, i pressed submit twise :p oops.

Anyway here are my classes: http://dc139.2shared.com/download/5304585/ce5e4915/Factories.zip?tsid=20090410-132655-6006a691

The download will start immediately.
kcaz - I've looked over your code.

You have a member b2Body *body in the base factory class, but in the constructors for the two derived classes, you create a local stack b2Body *body that you assign to.

This local variable hides the member body and is destroyed at the end of the constructor.

Your code is formatted all weird on my PC - for some reason there are no newlines so it is a bit hard to study in detail, but in essence, this is the problem:

Code:
class A
{
public:
    A();

    b2Body *body;
};

A::A()
{
    b2Body *body; // this is a different variable from the member "body"
    body=CreateABody(); // this operates on the local variable, not the member
}


HTH
What paul said sounds right.

If you're going to use XCode for C++, you might consider making it a habit to change your compiler warning settings, since the defaults can be a little too forgiving. They're located in Project->Edit Project Settings in the Build tab, all the way at the bottom. At least turn on the "Hidden Local Variables" and "Uninitialized Automatic Variables" warnings, as the bugs produced by errors of those sorts can be very difficult to track down; you might even want to turn on pedantic warnings, they can sometimes be useful (though after a while they might start to annoy you and you'll decide to turn them off).
Lol, well thanks for the tips, I'm going to try it now.

Original thread: https://box2d.org/forum/viewtopic.php?t=2907