Sign in to follow this  
BlackMoons

asIScriptObject::Release return code

Recommended Posts

BlackMoons    460

Hi. Today I managed to accidentally access a null handle in a Angelscript destructor called by my C++ program calling asIScriptObject::Release() on a script class object passed from Angelscript to C++.

 

The problem? It never logged anything in my error logs.

I go and check Release()'s return code and its 0.

 

Shouldn't it be returning asEXECUTION_EXCEPTION? And then I could handle it the same way as when Context->Execute() hits a null handle?

Share this post


Link to post
Share on other sites
WitchLord    4677
asIScriptObject::Release returns the number of remaining references to the object. In your case it returned zero so you released the last one which destroyed the object.

You're not really meant to use the return value from Release or AddRef. These are only returned for debugging purposes.

You only need to know that after calling Release you should no longer access the object from that pointer.

Share this post


Link to post
Share on other sites
BlackMoons    460

Ah, Then how should I detect when calling Release() results in a script exception and script execution of destructors being abandoned early?

 

I do understand that an exception in a destructor is a very bad thing that should be avoided at all costs due to potential for resource leaks, but that is exactly why I need to detect when it occurs so I can report the error and shut down the program so the code itself can be fixed.

Edited by BlackMoons

Share this post


Link to post
Share on other sites
BlackMoons    460

On exception, Raise a bool flag in the context, Allow checking that bool and if true they can get info of the exception using the normal script exception mechanism.

 

Yea, its kinda ugly, But something has to be done to allow it.

 

It might also allow people to have a centralized cache in there main loop for angelscript errors encase they ever forget to check the return code of normal executes(), or some other process raises an unexpected exception.

 

It beats a callback in that you can at least handle the error where it occurred and have some state information if you just assert()/log to text file on it or similar.

Share this post


Link to post
Share on other sites
WitchLord    4677

The problem is that the context that executes the destructor may not even be known by the application. If it is the garbage collector that calls it, then the garbage collector will create its own context for that execution.

 

I need to figure out a clean way of letting the application know about this internal context so that the application can set the exception callback, and even the line callback to allow the debugging of the destructor call.

Share this post


Link to post
Share on other sites
WitchLord    4677

The way I'm thinking about solving it is by having the engine invoke a callback from the application whenever it needs a context to execute some script code internally. The application can then supply a context that has been set up with the appropriate callbacks for catching exceptions. Not only would the application be able to report exception in the destructor this way, but it would also be able to debug the execution, and better pool the contexts to improve performannce.

 

It would also work better in a multithreaded environment as you could potentially have multiple contexts in parallel.

 

Of course, the application callback would be optional and if not provided the engine would create the context on its own as it currently does.

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