Box2D Forums

It is currently Sun May 19, 2013 6:57 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Mon May 14, 2012 8:37 pm 
Offline

Joined: Mon May 14, 2012 8:21 pm
Posts: 2
Hi everyone,

I'm having issues with a game I'm working on. I need to loop through all of the objects in the level, in order to update them (i.e., check if they are alive, respond to input, etc.), and I figured the best way to do this would be by making use of the body list already in the world:

Code:
for (b2Body *b = world->GetBodyList(); b; b = b->GetNext())
{
  GameObject *o = static_cast<GameObject*>(b->GetUserData());
  if (o)
    o->update();
}


It compiles fine, but it crashes when I call 'b->GetNext()':

Quote:
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.


I'm using Microsoft Visual Studio 2010. Any ideas why this is happening? I only ever attach objects inherited from GameObject to the b2Bodies' userdata, so that shouldn't be an issue. Is the b2Body list meant for this, or am I going to have to store a separate list of GameObjects to update them?

Edit: Upon further investigation and wolf fences, it looks like it's actually the 'o->update()' call that is causing the crash (without it, it runs fine). Has anyone ever encountered an error like this before? I don't know how to fix it -- I'm not using any special calling conventions as far as I know :| and I most definitely don't have any 'extern' or DLL stuff going on.

Thanks!
-atlas


Top
 Profile  
 
PostPosted: Mon May 14, 2012 11:45 pm 
Offline

Joined: Sun Oct 25, 2009 3:28 am
Posts: 242
It may be the static_cast. That runtime check failure might be caused by the fact that the UserData is not a GameObject (it may be a parent class, thus an incomplete object). Also, it may be calling a parent's virtual function (or the GameObject's virtual function) and not running the derived class's function. Simply put, somewhere along the way the pointer is pointing to something else.

If you can do dynamic_cast, try that to see if it's able to compile or yield a more helpful error message (like a null pointer exception).

If not, try setting the user data as a GameObject pointer and do an explicit (GameObject*) cast in that for loop instead of static_cast. If you're already storing the pointer as a GameObject pointer, then you don't need static_cast.


Top
 Profile  
 
PostPosted: Tue May 15, 2012 9:21 pm 
Offline
Site Admin

Joined: Thu Sep 06, 2007 12:34 am
Posts: 2931
Be sure the update function doesn't create or destroy a body.


Top
 Profile  
 
PostPosted: Wed May 16, 2012 8:39 pm 
Offline

Joined: Mon May 14, 2012 8:21 pm
Posts: 2
Thanks for your help.

Interestingly, the dynamic cast didn't work, as it said it was an "incomplete object". I usually use SetUserData(this) in the constructors of the classes that inherit GameObject (abstract class). I changed it so the SetUserData(this) is called in the GameObject constructor, and now it works. This seems kind of counter-intuitive to me, because the GameObject's update method is purely virtual, and it would make more sense if the inherited classes were the ones being set as the user data. I don't quite understand this behaviour, but at least it works.

Thanks again,
-atlas


Top
 Profile  
 
PostPosted: Wed May 16, 2012 10:42 pm 
Offline

Joined: Sun Oct 25, 2009 3:28 am
Posts: 242
Yeah, that's the problem with static_cast. If the actual class is not the class you're statically casting, it's going to call the class's functions as if they're not virtual. I can think of a reason for why it behaves like this, but you're right, it is counter-intuitive.

If you haven't tried already, you don't need static_cast; just explicitly cast it as (GameObject*).

Code:
GameObject *o = (GameObject*)(b->GetUserData());


The user data is a void*. You don't need to use static_cast or dynamic_cast. Just normally cast it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Bing [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group