Problem with setCustomForceAndTorqueCallback

TomUK

25-03-2008 11:14:39

Hi,

I'm writing a game which I have got to compile and run under windows and was trying to run it on linux as well. I've successfully compiled and linked it on linux but when I run it it crashes in the setCustomForceAndTorqueCallback function (it didn't do this on Windows).

I've got the latest version of OgreNewt from the CVS repository, Newton v1.53, Ogre v1.4.6. I compiled the OgreNewt code with a few printfs in OgreNewt::Body::setCustomForceAndTorqueCallback and it appears to crash in the if block when setting assigning callback to m_forceCallback. I'm fairly new to c++ so I could be doing something wrong but I've tried a few different ways:
1) body->setCustomForceAndTorqueCallback<MyClass>(&MyClass::forceCallback, this) from within the constuctor
2) body->setCustomForceAndTorqueCallback(boost::bind(&MyClass::forceCallback, this, _1)) in the constructor
3) the above but after the object has been constructed.
All three compile and then crash in the same place

I reconstructed it with the following code:
#include <OgreNewt_Body.h>
#include <OgreNewt_Collision.h>
#include <OgreNewt_CollisionPrimitives.h>
#include <OgreNewt_World.h>
#include <OgreVector3.h>

class Test
{
public:
OgreNewt::Body *b;
Test();
void callback(OgreNewt::Body *me);
} ;

Test::Test()
{
OgreNewt::World *world = new OgreNewt::World();
OgreNewt::Collision* col = new OgreNewt::CollisionPrimitives::Ellipsoid(world, Ogre::Vector3(1, 1, 1));
b = new OgreNewt::Body(world, col);
printf("Gets here\n");
b->setCustomForceAndTorqueCallback<Test>(&Test::callback, this);
printf("but not to here\n");
}

void Test::callback(OgreNewt::Body *me)
{
// do something interesting
}

int main(int argc, char **argv)
{
Test *t = new Test();
}


The output is:
Gets here
Segmentation fault (core dumped)

Am I doing something wrong? Is there any fix or way round it?

Thanks in advance

TomUK

25-03-2008 15:12:34

I changed the typedef for ForceCallBack in OgreNewt_Body.h to revision 1.1 (typedef void(*ForceCallback)(Body *me);) and changed my function to be static. It now works!

Either I'm using boost wrong, haven't installed it correctly or it has a bug (probably one of the first two :-) ). I'd still like it to work properly so if any help with what I may have done wrong or how to check if I've installed boost correctly would be good.

Thanks

TomUK

27-03-2008 17:44:54

I've got a very similar error but this time using the OgreNewt::World::setLeaveWorldCallback. I've recreated it again:

#include <OgreNewt_World.h>
#include <boost/bind.hpp>
#include <boost/function.hpp>

class Test
{
public:
Test();
void callback(OgreNewt::Body *me);
} ;

Test::Test()
{
OgreNewt::World *world = new OgreNewt::World();
printf("Gets here\n");
world->setLeaveWorldCallback<Test>(&Test::callback, this);
printf("but not here\n");
}

void Test::callback(OgreNewt::Body *me)
{
// do something interesting
}

int main(int argc, char **argv)
{
Test *t = new Test();
}


If I change the function call to:

//world->setLeaveWorldCallback(boost::bind(&Test::callback, this, _1));

it still crashes but if I do this:

boost::function<void(OgreNewt::Body*)> arg = boost::bind(&Test::callback, this, _1);
world->setLeaveWorldCallback(arg);


it works!?!

With the first way, OgreNewt uses boost::bind and boost::function internally so I can't see the difference between any of these methods.

I've tried different versions of boost and have previously tried different versions of OgreNewt. If anyone has any ideas, ie "did you do this when you installed ..." or "does your callback function do ..." or could try my code and let me know what they get I'd be grateful. I could probably get round it by doing a similar thing to the above problem but I don't think I should be changing libraries when it seems to work for everyone else!

Thanks

walaber

28-03-2008 19:19:41

try this:


world->setLeaveWorldCallback<Test>(&Test::callback, this);


there should be a templated function for setting the callback... iirc.

TomUK

29-03-2008 10:22:43

Hi

Thanks for the tip. Is that not what I tried in the third post's first example? The same code works for someone else in my group so I'm going to try and reinstall all the OgreNewt and boost libraries again.

walaber

29-03-2008 17:43:48

oh yeah, sorry... missed that :)

hopefully it's a boost-related issue...

TomUK

30-03-2008 15:47:22

I fixed the problem. For some reason the boost packages kept giving me segmentation faults. I removed anything to do with boost and then copied the boost folder from the ogrenewt repository to /usr/include and now it works :-) I know my code with the packages installed works fine on a friends PC but I've no idea why it didn't work for me.