• 15
• 15
• 11
• 9
• 10

# Crash on 32bit MinGW when function has value parameter

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

## 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 on other sites
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