🎉 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!

Reference problems

Started by
3 comments, last by WitchLord 19 years, 2 months ago
Hey I'm curious about how references are counted in AS. I have some vague memory of some docs somewhere saying that something that returns a handle needs to AddRef it first, and the same goes for stuff that passes handles into script functions (or something like that)? I can't find where it said it again, but I think I recall at least almost correctly. :) My problem is that the refcounting I already have in my game is pretty incompatible with this. In our system it's in all cases the receiving side that is responsible for the AddRef, which means it screws up together with AS. Functions that return objects never AddRef, and then when AS releases the handle the refcount goes odd. I'm not sure at all how to solve this. I'm not very keen on creating wrappers for everything, since the lack of need for wrappers is one of the reasons I like AS in the first place. I currently just AddRef like crazy just to keep things alive, which pretty much means nothing ever gets removed. That keeps the problem from being a total showstopper, but I'd rather get something working pretty quick. Any ideas? /Anders Stenberg Edit: Maybe it would be possible to add the possibility to set some kind of AddRef/Release rules on the AS engine? I mean explicitly tell it in what cases it should AddRef/Release and not? (Just a stray idea.)
Advertisement
1. When a object constructor returns it should have 1 reference counted already

2. When returning an object handle from an application function the application should account for the handle.

3. When receiving an object handle in a parameter, the application function should release the handle before returning.

4. When calling a script function from the application, the methods SetArgObject() and GetReturnObject() already handles the reference counting for you.

5. When implementing the application function using the asCALL_GENERIC calling convention the methods GetArgObject() and SetReturnObject() already handles the reference counting for you.

---

I choose these rules because they were the least troublesome, and also it is the way COM+ does it. I found that another implementation could cause inconsistencies, which required the application to make work-arounds, for example if an object handle needs to be returned that shouldn't be stored in any other location.

I realize that not all applications do their reference counting using these rules, which is why I plan on implementing hints for AngelScript that tells the library to automatically release parameter handles when the function returns and also to increase the refcounter for returned objects.

My current plans are that you'll simply register the function as follows:

"obj @+ func( obj @+ )"

And the library will know that the function will not release the parameter by itself, and also that the returned object won't have the reference counted. This will also help when calling functions that don't even know about reference counters.

I don't know when this will be implemented though, but if I can squeeze it in I will do it for version 2.2.0.

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

Something like that would be awesome! (Provided it can handle my situations, which I think it can. My brain is not active enough to be bothered with really thinking it through, so I trust you on it. :)

/Anders Stenberg
Hi

I realized my need for this is more urgent than I initially anticipated. (I currently have pretty much zero control over the lifetime of objects. They live on forever due to excessive addreffing). Have you got any ideas on workarounds or so until something proper comes along?
The only work around I can think of is to use wrappers that correctly adjusts the reference counter.

You could also try to implement the hints I mentioned in AngelScript. It shouldn't be too difficult, though it requires knowledge of how AngelScript works.

I may try to implement the hints myself for the 2.2.0 version, though I can't promise it.

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