Sign in to follow this  
villemon

Reference counting

Recommended Posts

Hello again WitchLord and fellow Angelscript fans. :) I wonder if I'm doing something wrong with the reference counting. Here's the deal: I have my own class that I've registered with RegisterObjectType. I've also registered reference counting behavior, but not assignment or anything else. In the constructor for the class I set the reference count to 1. Addref and Release look like this: int UIEntity::AddRef() { return ++refCount; } int UIEntity::Release() { if( --refCount == 0 ) { delete this; return 0; } return refCount; } When I create an instance of this class from the script, I do it from a global function, that basically looks like this. The function is registered to pass a handle. UIEntity* ScriptInterface::UICreateText(parameters...){ UIEntity* entity=new UIEntity(); return entity; } The script that uses this looks like following: UIEntity @healthMeter;//global variable void initialize(parameters...) { @healthMeter=@UICreateText(parameters...); } Now for the problem. The reference for the newly created UIEntity is released too many times, and the final time it is released obviously has an invalid pointer to it since it's been deleted already. The final call comes from ResetModule, which I've got in the end of my program. Now I haven't seen a guide to reference counting anywhere, does one exist? Should I for example add to the reference every time I pass a handle of the object to the script? Should I add addRef to UICreateText? I'd appreciate any help. Maybe if no reference counting manual exists, I or somebody could write it in the wiki.

Share this post


Link to post
Share on other sites
Thanks Mandrav, following that lead led me to this thread: http://www.gamedev.net/community/forums/topic.asp?topic_id=312968

Interestingly enough, the post by WitchLord explains the reference counting much better than the Angelscript documentation. Time to update the documentation?

Share this post


Link to post
Share on other sites
Indeed. The documentation needs a revision, I just can't find the time and energy to do it.

I'm not sure if any doubts remain as to your original question, but just in case I'll answer it anyway.

First of all: If you intend to do assignment by value, then you must register the assignment behaviour, otherwise AngelScript will do a byte-for-byte copy of the object, including the reference counter.

Secondly: When passing an object handle to the script, the reference you're passing should be accounted for. I.e. the newly constructed object in UICreateText() should have a refCount == 1.

Likewise, any handles you receive from the script will have a reference counted, that must be released when the application no longer need the references.

If you'd like to update the wiki with this information, I'd be grateful. I'm currently quite short on time, otherwise I would do it myself.

The @+ that mandrav mentioned is called an autohandle. When AngelScript sees these for registered methods it automatically does the necessary increment/decrement of references. I recommend using these only for functions/methods where you're not able to change the code to manually do the proper treatment.

Regards,
Andreas

Share this post


Link to post
Share on other sites
For anyone still reading this thread:

I found a little time over and decided to start updating the wiki. The first informational page talks about object handles and how the reference counter should be managed for them.

This page may also serve as a template for anyone wishing to contribute with more information.

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