Crashing when getting value from userdata.

Okay so lets say I have a data structure like this:
Code:
struct MyBodyData
{
   int someNumber;
};


Then I later add a body with it as userdata like this:
Code:
b2BodyDef bodyDef;
bodyDef.position.Set(0.0f, -10.0f);

b2Body* body = world.CreateBody(&bodyDef);
MyBodyData bodyData;
bodyData.someNumber = 4;
body->SetUserData(&bodyData);

b2PolygonDef shapeDef;
shapeDef.SetAsBox(50.0f, 10.0f);
body->CreateShape(&shapeDef);


Then later in my code I want to access someNumber, so I do something like this:
Code:
for (b2Body* b = world.GetBodyList(); b; b = b->GetNext())
{
   MyBodyData* data = (MyBodyData*)b->GetUserData();
      
   if (data->someNumber == 4)
   {
      //do something
   }
}


Now... When I try to access someNumber like this everything compiles just fine, but my program will crash ("Box2dTest.exe has encountered a problem and needs to close. We are sorry for the inconvenience."). If I comment out the if statement where I try to access data->someNumber, everything runs fine.
If anyone could tell me what I'm doing wrong, it would be greatly appreciated =).

Oh and I hope I gave enough information... if you need any more, I'll do my best.

Thanks!
Here you're creating the bodyData instance on the stack:
Code:
MyBodyData bodyData;
bodyData.someNumber = 4;
body->SetUserData(&bodyData);

That means that once it goes out of scope, it's no longer there: the body user data points to garbage. You need to allocate bodyData on the heap instead (with malloc/new) and store the pointer so you can later free the memory when it's no longer needed. 🙂
Thanks for the reply! But I'm still having some problems...

From what I understood out of what you said, I should have changed
Code:
MyBodyData bodyData;
bodyData.someNumber = 4;
body->SetUserData(&bodyData);


To
Code:
MyBodyData* bodyData = new MyBodyData;
bodyData->someNumber = 4;
groundBody->SetUserData(bodyData);


I did this, and its still crashing when I try to access the userdata. Am I still doing this wrong?

I also tried creating bodyData in global scope, up at the top outside of main(), when I declare my struct, like this:
Code:
struct MyBodyData
{
   int someNumber;
} bodyData;


And then using this again:
Code:
bodyData.someNumber = 4;
body->SetUserData(&bodyData);


And it still crashes.

I then was thinking that its being caused somewhere else in my code... So for testing purposes I tried it in the hello world example and it still compiles just fine, but crashes when I try to access the userdata.

Hopefully this is just a simple mistake on my part... And again, thanks for the reply!
Stuna213 wrote:
Thanks for the reply! But I'm still having some problems...

Hmm. Interesting. 🙂 Have you tried setting the user data for the BodyDef instead of the Body?

Code:
b2BodyDef bodyDef;
bodyDef.position.Set(0.0f, -10.0f);

MyBodyData bodyData = new ....
bodyDef.userData = bodyData;

b2Body* body = world.CreateBody(&bodyDef);
....
Tried that as well, same problem still =X.

Edit: I think the problem must be with the way I'm stepping through the world, becuase if I comment out that part and just get the userdata with body->GetUserData() it works just fine.
mh114's first comment is quite correct, you need to use new MyBodyData().
But that is not your only problem.
What is causing failure now is that you have only set the user data for one body, not every body in the system. And yet, you are looping over every body, and asking about it's user data. It's crashing when you do data->someNumber for some other body, because data will not be valid.

The solution is to check first that data is valid, before you do anything further with it:
Code:
for (b2Body* b = world.GetBodyList(); b; b = b->GetNext())
{
   MyBodyData* data = (MyBodyData*)b->GetUserData();
     
   if (data!=0 && data->someNumber == 4)
   {
      //do something
   }
}


This works as the pointer for the user data is initialized to 0 (null) to begin with.
Remember you also need to delete the structs created with new, or else you will get memory leaks.
Aha! I knew it must have been something simple like that. Thanks so much for your help, you two!

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