Jump to content
  • Advertisement
Sign in to follow this  
AgentC

Crash on 32bit MinGW when function has value parameter

This topic is 2696 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've come across a MinGW/GCC optimization that causes a crash.. There's this C++ function which takes a value parameter (Vector3 is a 12 bytes POD type)


void PhysicsWorld::SetGravity(Vector3 gravity)
{
dWorldSetGravity(physicsWorld_, gravity.x_, gravity.y_, gravity.z_);
}


Normally the Vector3 should of course be passed by const reference, but it's now being passed by value due to a templated auto-serialization system that also calls this function.

Now, when the function is exposed to Angelscript normally, it will crash on MinGW when called, as the compiler actually optimized it to a reference, and instead of reading the Vector3 data on the stack as expected, it interprets it as a pointer to Vector3:


engine->RegisterObjectMethod("PhysicsWorld", "void set_gravity(Vector3)", asMETHOD(PhysicsWorld, SetGravity), asCALL_THISCALL);


What I currently do to work around this is somewhat ugly:


#ifdef _MSC_VER
engine->RegisterObjectMethod("PhysicsWorld", "void set_gravity(Vector3)", asMETHOD(PhysicsWorld, SetGravity), asCALL_THISCALL);
#else
// GCC will convert to reference
engine->RegisterObjectMethod("PhysicsWorld", "void set_gravity(const Vector3&in)", asMETHOD(PhysicsWorld, SetGravity), asCALL_THISCALL);
#endif


This is of course not a fault of AngelScript, but rather, I'm asking if anyone knows a GCC flag that will disable this optimization?

Share this post


Link to post
Share on other sites
Advertisement
This looks like a problem with the native calling convention, rather than a bug in MinGW. The MinGW compiler optimization must not change the way parameters are sent to a function as it cannot know who is calling it, especially when function pointers are involved.In GCC, and consequently MinGW, all classes that has either a destructor or a copy constructor are always passed by reference even if the function declares it as being sent by value.In 2.20.3 that is in the SVN I've introduced a new flag for registering objects: asOBJ_APP_CLASS_COPY_CONSTRUCTOR. I believe you need to use that when registering the Vector3 class, e.g:engine->RegisterObjectType("Vector3", sizeof(Vector3), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CAK); // K is for the copy constructorPlease let me know if this solves your problem. If not there might be something else that needs to be adjusted.Regards,Andreas

Share this post


Link to post
Share on other sites
Thanks, that was it indeed. Uncovered quite some value type registration bugs from my code in the process, turned out that I just had not exercised them before :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!