Sign in to follow this  

asBEHAVE_RELEASE couldn't register with a virtual function!

Recommended Posts

loboWu    214
in C++ class CBase { public: CBase(){} virtual ~CBase() {} int a; virtual void AddRef() {} virtual void Release() {} }; class CMytype: public CBase { public: CMyType(); virtual ~CMyType(); virtual void Release() {} }; CMytype global; CMytype* GetMytype() { return &global; } and In AS, register it with a type { r = engine->RegisterObjectType("Mytype", 0, asOBJ_REF); assert( r >= 0 ); r = engine->RegisterObjectBehaviour("Mytype", asBEHAVE_ADDREF, "void f()", asMETHOD(CMytype, AddRef), asCALL_THISCALL); assert( r >= 0 ); r = engine->RegisterObjectBehaviour("Mytype", asBEHAVE_RELEASE, "void f()", asMETHOD(CMytype, Release), asCALL_THISCALL); assert( r >= 0 ); r = engine->RegisterObjectProperty("Mytype", "int a", offsetof(CMytype, a)); assert( r >= 0 ); r = engine->RegisterGlobalFunction("Mytype @GetMytype()", asFUNCTION(GetMytype), asCALL_CDECL); assert( r >= 0 ); } and I use also register the "any" type and write a sample script { Mytype @test = @GetMytype(); any anytest;; } //after running script, it will call test->Release() to release the object, //but it will raise an exception! But if I change the virtual Release() to non virtual, it is OK! Am I wrong? or it is just a bug? Maybe I need to change asCALL_THISCALL to another flag, but I am a little confuse about asCALL_THISCALL, asCALL_CDECL. Thanks.

Share this post

Link to post
Share on other sites
WitchLord    4677
I wasn't able to reproduce the problem that you reported. Either the problem has already been fixed, or the code you gave isn't enough to reproduce it.

I noticed that the code you gave have some minor errors in it that makes it fail to compile. It's nothing big but it's obviously not the same code that you had when encountering the problem. Could you give me another piece of code that compiles and reproduces the problem?


You're registering the Mytype correctly, and AngelScript fully supports virtual methods.

The problem is probably not with the virtual call itself, but rather that the object pointer is invalid for some reason. If the object pointer is invalid a virtual method would fail because the virtual function table would also be invalid. A non-virtual method would succeed though (unless it tries to access a member of the object).

As to your doubt about asCALL_THISCALL and asCALL_CDECL. For class methods you should use asCALL_THISCALL, and for global functions you should use asCALL_CDECL. AngelScript will give an error if you do it incorrectly, so don't worry about it too much.

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