Advertisement

Generic vs Native call convention reference counts

Started by May 06, 2009 05:45 AM
2 comments, last by WitchLord 15 years, 6 months ago
Hi, I've recently converted the scripting code from native calling convention to generic one for 64bit compilation (I am using Visual Studio 2005, Vista, 64bit). In the previous version (on 32bit), when I've registered a class to the script I've used a starting reference count of 1 in the class constructor. Then asBEHAVE_ADDREF and asBEHAVE_RELEASE behaviors to keep the correct reference count. I've also have a factory (asBEHAVE_RELEASE) which allocated the class and so called the constructor (so the reference count started with 1). Also on 32bit (native call) I needed to manually call the Unref procedure at the end of each script registered function that received a reference variable of that class type. Is this normal behavior? I am asking this since for generic calling I don't need to call the Unref. It is automatically called by AngelScript when exiting the registered function. I've found this because my program crashed (reference count reached zero ahead of the time). Also, another difference, in generic convention my class factory is something like this:

	void Factory_MyClass(iScriptFuncParams *params)
	{
		MyClass *cl = new MyClass();
		params->SetReturnObject(cl);
	}
The SetReturnObject automatically increases the reference count so I need to start it as zero in my class construction. On the other hand, as described above, for native calling I need to start it as 1 since there is no SetReturnObject call in the factory:

	MyClass* Factory_MyClass()
	{
		return new MyClass;
	}
Regards, Adrian L.
I'm sorry about these differences. I know it can be confusing, and I have plans to fix it in the near future (not just for the sake of the differences, but for better performance too).

The native calling conventions currently have to manually release handles they receive, except if you register the function with auto-handles. For the generic calling conventions, the asIScriptGeneric is the owner of the handle and will automatically release it when the function returns.

As for the factory, you shouldn't change your initial refCount, instead use the appropriate method of the asIScriptGeneric to return the handle. I personally prefer GetAddressOfReturnLocation, but you can use SetReturnAddress as well.

void Factory_MyClass(asIScriptGeneric *gen){  MyClass *cl = new MyClass();  *((MyClass**)params->GetAddressOfReturnLocation()) = cl;}


You may also want to take a look at the auto wrapper add-on. It can greatly simplify your work as you won't have to manually write all the wrappers.

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
Thank you for clarifications. Yes, maybe the best approach is to document the differences (at least until of unification) so other people will find it easier to use the generic approach. I've personally used the standard string registration as example since there was no extensive documentation of iScriptFuncsParams. Also thank you for the tip with the class factory.

In case the reference count behavior changes, please let us know (through the change log file) since we regularly upgrade the library.

Regards,
Adrian L.

BTW thank you again for this great library, it works perfectly now on both 32 and 64bit systems.
It is documented here and here, however I'll see if I can highlight the differences a bit better.

Yes, of course I'll let you know when I make the changes. I wouldn't want to surprise anyone. It will probably be available through a preprocessor flag at first, so that users will get time to adapt their code before I make the final switch.

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