Jump to content
  • Advertisement
Sign in to follow this  
BlackMoons

Function Reference counting

This topic is 2058 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!