asIScriptObject::Release return code

Started by
8 comments, last by WitchLord 9 years, 11 months ago

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?

Lead Coder/Game Designer for Brutal Nature: http://BrutalNature.com

Advertisement
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.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

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.

Lead Coder/Game Designer for Brutal Nature: http://BrutalNature.com

That same question was asked a year ago, but unfortunately I haven't thought of an adequate solution to it yet.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

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.

Lead Coder/Game Designer for Brutal Nature: http://BrutalNature.com

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.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Hold a ref to the last erroring context in the engine itself and have the bool in the engine?

Lead Coder/Game Designer for Brutal Nature: http://BrutalNature.com

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.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Ah that would be nice. Yea I would prefer that angelscript just reuse the one context I have with push/pop state and minimize run time allocations.

Lead Coder/Game Designer for Brutal Nature: http://BrutalNature.com

I've implemented the support for this in revision 1929. See the description in the other thread for a brief overview of how it works.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement