Advertisement

Weak Reference Handles

Started by March 21, 2011 04:39 PM
4 comments, last by WitchLord 13 years, 11 months ago
Hell there,

I have been using AngelScript and so far am really enjoying the process.
For my work, I need to ensure that when object gets destroyed, all the handles become null.

I have attempted to define asBEHAVE_ADDREF and asBEHAVE_RELEASE and track references by type of object (if its handle or real object), however I cant seem to be able to access the target object (I use asCALL_GENERIC type)

Here is what I am looking for:


Object a;
Object b;
Object@ c;

a=b;
@c=a;
a.release(); // b and c are valid
b.release(); // c becomes null



Is there anything I could do to resolve the issue?
I recomend using proxies for these classes that can be destroyed at any time. The real object will only have to keep track of a single proxy. And all references to the object really point to the proxy. When the object is destroyed it simply updates the proxy to say the object is dead. The proxy can then have a default behaviour or graciously fail whenever the script tries to access the dead object.

This is the way I do it in my own game engine. And not just for the script binding, but also between different parts of the game engine. I find it really convenient to not have to worry about finding all referenses to the object when it has to be destroyed.

Regards,
Andreas

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

Advertisement
Hello Andreas,

My issue is that I want to have multiple objects and keep object alive till all the copies of it are still up.

I have moved a little further. By overloading

Object &opAssign(const Object &in)


I can now detect when copy of the object is made.
Handles are tracked through asBEHAVE_ADDREF. However both handles and objects get destroyed using asBEHAVE_RELEASE.
If I could only tell the difference between handle and object during asBEHAVE_RELEASE, my method would be perfect.

Any ideas?

Thanks
Hmm. So you want to keep track of all copies of the same object as well as all references to any of these copies? That is quite unusual. May I ask why you want to do that?

Keeping track of copies should preferably be done by the actual application class. You would need some way of separating two different instances, maybe some internal id. You could then map this id to a counter, and have the class' constructor increment the counter, the destructor decrement it, and the assignment operator decrement the old id and increment the new one.

The reference counter updated with addref/release should be per instance. The constructor should initiate the reference counter to 1. Then the addref increments it, and release decrements it. When the reference counter reaches zero in the release method, the instance should be destroyed by deleting the this object (this will in turn invoke the class' destructor which will decrement the number of copies of the object).The assignment operator doesn't change the reference counter in either of the objects, so make sure you don't accidentally copy the reference counter or you'll likely get application crashes.


Regards,

Andreas

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

Hello Andreas,

Actually, I am not really interested in number of handles, just copies of object. I would like to treat copies as if they have strong reference, and handles as weak reference.
So if an object is being referenced by two other objects, it can not be destroyed until both objects remove the reference. Once it is destroyed, regardless of how many handles reference it, they will become null.

Internally, I already have thing working and things are working well, I am just trying to mimic the functionality in AngelScript.

Thanks
Can you show me how you've implemented this internally in your application? I think that would help in trying to figure out how you might be able to mimic it in AngelScript.

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