SetTarget and for loops?

Hi guys,

I am having a weird problem and I can't seem to figure out why it's happening and how I am going to fix it.

I am coding in C++ with Box2D in CodeBlocks with the GNC GCC Compiler.

Whenever I use a for loop in my game, it crashes when I try and use a mouseJoint. With a little bit of investigation I narrowed down the problem to whenever I call SetTarget().

The setTarget isn't inside the for loop, in fact as far as I can see, the for loop and the SetTarget don't have anything to do with each other. When I try and drag something using a mouseJoint, the game freezes and eventually closes, telling me:

Code:
Process returned -1073741819 (0xC0000005)


I am stumped on this one, anyone have any ideas?

Thanks.
If you run your program in a debugger, it'll be able to give you much more accurate information. I'm going to ask the obvious question that you are quite sure that the joint pointer is valid at the time of calling set target?
I had a similar problem. I was accidentally using "new" to create the joints, and incorrectly set up the joint definition.

If you can show us the mouse joint code maybe we can help you a bit more easily!

-P
The joint pointer is valid yes.

The thing that is confusing me is that the code works absolutely fine, IF I don't have a for loop. But then if I put a for loop anywhere in the code, SetTarget() causes the system to crash.

I have run a debugger, which tells me:
Code:
Program received signal SIGSEGV, Segmentation fault.
In b2MouseJoint::SetTarget(b2Vec2 const&) (this=0x4, target=@0x2168f0) at C:/Developer Libraries/Box2D_v2.0.1/Box2D/Source/Dynamics/Joints/b2MouseJoint.cpp:59 ()


Here is my mouseJoint code:
Code:
b2MouseJoint* dotDrag;

                //deal with mouse dragging
                if(mouseDrag)
                {
                    b2MouseJointDef dotDragDef;
                    dotDragDef.body1 = world.GetGroundBody();
                    dotDragDef.body2 = body;
                    dotDragDef.timeStep = timeStep;
                    dotDragDef.dampingRatio = 0;
                    dotDragDef.frequencyHz = 60.0;
                    dotDragDef.target.Set(pointerPos.x, pointerPos.y);
                    dotDragDef.maxForce = 100000.0f;

                    if(dragOn)
                    {
                        printf("problem?");
                        dotDrag->SetTarget(pointerPos);//<--HERE IS THE CRASH ISSUE WHEN USING FOR LOOPS
                        printf(" no!\n");
                    }
                    else
                    {
                        dotDrag = static_cast<b2MouseJoint*>(world.CreateJoint(&dotDragDef));
                        dragOn = true;
                    }
                }
                else if(dragOn)
                {
                    hold(body);
                    world.DestroyJoint(dotDrag);
                    dragOn = false;
                }


Thanks.
Quote:
this=0x4

Doesn't look like a valid pointer to me.

Crashing if you have a for loop anywhere is a nonsensical condition. I still remain convinced there is a stupid error in your code.

I'd be clutching at straws, but you might want to try a C-style cast instead of a static_cast. This is what Box2D uses internally (oldschool, I know), and it's maybe possible that they work differently on some compilers?
Sounds like a memory consistency error of some sort, something is getting corrupted somewhere, and it likely has very little to do with your loop. The presence of the loop is just growing your stack frame to a size such that you're now touching the corrupted memory (or it's otherwise changing the layout of memory so that you're having problems).

Try a few things:

1) Check any array accesses, make sure you're not accessing any arrays past their bounds
2) Make sure you've properly initialized all local and class variables
3) Make sure you haven't called delete/free twice on any heap allocated object
4) If you've got your own classes, double check your copy and assignment operators, especially if you're allocating any memory to the object itself; it's very easy to make mistakes in these operators that will result in memory problems.
BorisTheBrave wrote:
Crashing if you have a for loop anywhere is a nonsensical condition. I still remain convinced there is a stupid error in your code.


I know, it doesn't make sense to me either.

Just as an example, here is one of the for loops I am using:
Code:
for(int i=0;i<4;i++)
{
        gameApp.Draw(wallDisplay.at(i));
}


When this is included, the program crashes when SetTarget is called.

But when I instead use:
Code:
gameApp.Draw(wallDisplay.at(0));
gameApp.Draw(wallDisplay.at(1));
gameApp.Draw(wallDisplay.at(2));
gameApp.Draw(wallDisplay.at(3));


The program works fine.

ewjordan wrote:
Sounds like a memory consistency error of some sort, something is getting corrupted somewhere, and it likely has very little to do with your loop. The presence of the loop is just growing your stack frame to a size such that you're now touching the corrupted memory (or it's otherwise changing the layout of memory so that you're having problems).


As you can see, I am using vectors in my loops. Not having used vectors before, I can't for sure say I'm using them correctly, but I've followed a guide as best I can and I think I am. Could this be causing my problem?
ewjordan's saying it doesn't even have to be the loops themselves that our problematic. Memory corruption bugs are fiddly like that, as they can come from unrelated areas. Short of using a memory fence debugger, you need to scrutinize dodgy code, which usually means pointer stuff. It's pretty damn hard to go wrong with vectors, except walking off the end of a vector much like you can an array, so I'd look over ewjordan's suggested areas first.
Now that you've mentioned using vectors, I would definitely take a look through all your classes and make sure you're not doing anything wrong in any of your constructors, destructors, copy and assignment operators. STL containers have been known to do some copying behind the scenes (for instance, on a capacity resize), and if you've got an allocation/deallocation bug somewhere in a class that's getting stored in a vector, things might blow up on you even if you don't think you're actually creating or destroying any objects.

But as Boris said, the problem is not necessarily inside one of your for loops, it could literally be anywhere and is just showing up because the for loop changes the memory layout slightly, which is one of the maddening things about C++.
Okay, well I haven't created any of my own classes, but I am using a few vectors and resizing them rapidly.

I'll go through them and see what I can find. I'll try an electric fence and see if that helps.

Thanks guys, I'll let you know how it goes...

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