🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Handles again :-)

Started by
0 comments, last by WitchLord 19 years, 2 months ago
Hi! It's me again :-) I have another question about handles. I have to make sure about a few things, our AS scripts will be written by people not very familiar with AngelScript, and I have to make everything safe for them :-) Here's a AS code snippet:

WindLevel wl(1.1, 4.5);
WindArea wa();

wa.AddWindLevel(@wl);

WindLevel@ wl2;
@wl2 = @wa.GetWindLevel(0);
WindLevel class has ctor/dtor and addref/release behaviours registered. WindArea::GetWindLevel calls AddRef upon succesful lookup. This works as expected. However, when I change the code to this:

WindLevel wl(1.1, 4.5);
WindArea wa();

wa.AddWindLevel(@wl);
WindLevel wl2(1.0, 1.0); // no default ctor, we must call non-default ctor for proper addref count

wl2 = wa.GetWindLevel(0);
The problem here is easily overlooked. WindLevel doesn't have the assignment behaviour registered and the last assignment simply does a shallow copy, including the refcount variable. My guess is that I have to register the assignment behaviour that doesn't copy the refcount variable. Is there anything else I should be aware of?
Advertisement
For handles to work perfectly for a reference counted c++ class you need to register the following behaviours:

asBEHAVE_CONSTRUCT - initializes the refCount to 1
asBEHAVE_ADDREF - increases the refCount
asBEHAVE_RELEASE - decreases the refCount and frees the object if it reaches 0
asBEHAVE_ASSIGNMENT - copies the object content, but not the refCount

That is all you need to register. The destructor doesn't have to be registered as it is never called by the library, since release() is used instead.

Also you need to make sure the refCount is properly managed by any system functions. Objects received by handle in parameters must be released when no longer used. Objects returned by handle must have the refCount increased when returning.

You can take a look at the asCScriptString, or the objects registered in my Texture Generator, for working examples on reference counted objects.

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

This topic is closed to new replies.

Advertisement