I think the problem is with the constructor/destructor behaviour. Since my test only compiled the script, but didn't execute it I didn't notice an error that I made.
Since the constructor/destructor behaviours are implemented as class methods, the object pointer to work on is stored in the this pointer, not in any of the method arguments as I had implemented in my test. I changed this in my test, and also added a CEngine as global variable in the script to test this.
class CEngine{public: CEngine() {}; ~CEngine() {}; void ShowMessage(std::string &) {} void Find(std::string &) {} void LoadExtension(std::string &) {} void Constructor() { new(this) CEngine(); } void Destructor() { this->~CEngine(); }};bool Test2(){ bool fail = false; COutStream out; asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION); int r = engine->SetMessageCallback(asMETHOD(COutStream, Callback), &out, asCALL_THISCALL); assert( r >= 0 ); RegisterScriptString(engine); r = engine->RegisterObjectType("CEngine", sizeof(CEngine), asOBJ_VALUE | asOBJ_APP_CLASS_CDA); assert( r >= 0 ); r = engine->RegisterObjectBehaviour("CEngine", asBEHAVE_CONSTRUCT, "void f()", asMETHOD(CEngine, Constructor), asCALL_THISCALL); assert(r >= 0); r = engine->RegisterObjectBehaviour("CEngine", asBEHAVE_DESTRUCT, "void f()", asMETHOD(CEngine, Destructor), asCALL_THISCALL); assert(r >= 0); r = engine->RegisterObjectMethod("CEngine", "void ShowMessage(string &in)", asMETHOD(CEngine, ShowMessage), asCALL_THISCALL); assert( r >= 0 ); r = engine->RegisterObjectMethod("CEngine", "void Find(string &in)", asMETHOD(CEngine, Find), asCALL_THISCALL); assert( r >= 0 ); r = engine->RegisterObjectMethod("CEngine", "void LoadExtension(string &in)", asMETHOD(CEngine, LoadExtension), asCALL_THISCALL); assert( r >= 0 ); const char * script = "CEngine g; \n" "void main() \n" "{ \n" "CEngine blah; \n" "} \n"; asIScriptModule *mod = engine->GetModule("mod", asGM_ALWAYS_CREATE); mod->AddScriptSection("script", script); r = mod->Build(); if( r < 0 ) { fail = true; } engine->Release(); return fail;}
The above works, without presenting any of the problems you mentioned.
How have you implemented the constructor and destructor behaviour methods? I recommend you implement them as global functions, or static member methods, in which case they would look like this:
class CLoad{ static Constructor(void *p) { new(p) CEngine(); } static Destructor(CEngine *p) { p->~CEngine(); }}r = engine->RegisterObjectBehaviour("CEngine", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(CLoad::Constructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );r = engine->RegisterObjectBehaviour("CEngine", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(CLoad::Destructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );