Sign in to follow this  

Angelscript copy constructor crash

Recommended Posts



I'm learning how to bind C++ classes to Angelscript, but I've run into a problem, when adding a copy constructor to a class, the application crashes. I'm pretty sure that I'm doing something wrong because I can't seem to find an example on how to do this properly.


The class I'm registering is a simple 3D vector with 3 floats (x,y,z).


The class registration:

int r = engine->RegisterObjectType("vec3f", sizeof(vec3f), asOBJ_VALUE | asOBJ_APP_CLASS_CDAK);


copy constructor registration:

r = engine->RegisterObjectBehaviour("vec3f", asBEHAVE_CONSTRUCT, "void f(const vec3f &in)", asFUNCTION(vec3fCopyConstructor), asCALL_CDECL_OBJLAST);


the function 'vec3fCopyConstructor':

void vec3fCopyConstructor(void *memory, const vec3f& other)
	new(memory) vec3f(other);


Thanks a lot in advance for any help,

Share this post

Link to post
Share on other sites

Hi assainator,


The problem is because you inverted the order of the arguments to the vec3fCopyConstructor. You registered it with asCALL_CDECL_OBJLAST, but the object pointer (in your case memory) is the first argument. 


Either change the order of the arguments, or change the calling convention to asCALL_CDECL_OBJFIRST. :)


If you want an example to follow I suggest you take a look at the script math complex add-on that you'll find in the folder sdk/add_on/scriptmath/




Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this