Advertisement

Function Reference counting

Started by February 23, 2014 10:28 PM
1 comment, last by BlackMoons 10 years, 9 months ago

Hi. I am wondering how its recommended I deal with function ref counts. I am getting some cleanup errors on ref

counts.


(0:0) Object {107}. GC cannot destroy an object of type '_builtin_function_' as it can't see all references. Current ref count is 1.
(0:0) The builtin type in previous message is named 'InventoryClosedCallback'

Mainly I am wondering if 'module->GetFunctionByName()' adds a refcount to the function it returns?

Should I be addref'ing what it returns before storing it? Should I release on program cleanup? Or should I just let angelscript handle all the ref counts for functions and not touch them?

Currently I have:


asIScriptModule *module = scriptEngine->GetModule(moduleNameAscii.c_str());
asIScriptFunction *func = module->GetFunctionByName(functionNameAscii.c_str());
func->AddRef();
AddScriptCallback(func)

AddScriptCallback just adds it to a vector of asIScriptFunction*, who get release() called on them on program exit (Before anglescript is cleaned up however)

if I remove the func->AddRef, angelscript crashes in asCModule::~asCModule() Line 68, called from asCScriptEngine::~asCScriptEngine() Line 559 (ie, as angelscript shuts down)

It also appears that when Angelscript calls a function similar to AddScriptCallback that the func does indeed have 1 extra ref. I assume that is because I pass it by @ handle.


void Player::AddItemCallback(ScriptCallback::CallbackSource callbackSource,  asIScriptFunction *func, unsigned int callbackData)
{
	ScriptCallback newCallback;
	newCallback.mCallback = func;
	newCallback.mCallbackData = callbackData;
	newCallback.mCallbackSource = callbackSource;
	mScriptCallbacks.Add(newCallback);
}

r = scriptEngine->RegisterObjectMethod("Player", "void AddItemCallback(CallbackSource,ItemCallback @,uint)", asMETHOD(Player,AddItemCallback), asCALL_THISCALL); assert( r >= 0 );

that results in angelscript not complaining about any GC cleanup errors.

Lead Coder/Game Designer for Brutal Nature: http://BrutalNature.com

GetFunctionByName() does not increment the refCount. None of the Get... methods increment the refCount of whatever object is returned.

If you increment the refCount by calling AddRef() you must also release it later with Release().

You'll need to increment the refCount of the function if, you must guarantee that it stays alive even if the script is discarded or recompiled. If your application is guaranteed not to use the script function after the module has been discarded or recompiled, then there is no need to increment the refCount and consequently not release it either.

Somewhere there is a missing Release(). Do you perhaps have someplace in the code where the script callback function is exchanged for another and you forgot to release the previous object before reassigning the pointer?

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

I believe I figured it out, I found a small typo in my cleanup code that was skipping over that array.

(for(;cur != cur; cur++) vs for(; cur != end; cur++))

Thanks for the assistance.

Lead Coder/Game Designer for Brutal Nature: http://BrutalNature.com

This topic is closed to new replies.

Advertisement