Delegates created by scripts don't return the module they were made in

Started by
2 comments, last by Solokiller 1 year, 7 months ago

When a native API gets an asIScriptFunction that is a delegate, GetModule() returns null instead of the module the underlying function is implemented in.

This complicates cleanup when discarding modules. For example this code is supposed to remove all callbacks that originate from the given module:

m_Functions.erase(std::remove_if(m_Functions.begin(), m_Functions.end(), [&](const auto& candidate)
						  { return candidate->m_Function->GetModule() == &module; }),
		m_Functions.end());

But because delegates return null here they aren't removed. This then causes a crash later on when the callback is cleared because the object instance has already been freed but is still processed by the garbage collector here:

else if( engine->CallObjectMethodRetInt(gcObj.obj, gcObj.type->beh.gcGetRefCount) == 1 )

(Note that in this case the script context also holds a reference, the crash occurs right after the callback is invoked and removed)

If this is intended behavior then it should be noted in the documentation here: https://www.angelcode.com/angelscript/sdk/docs/manual/doc_callbacks.html#doc_callbacks_delegate

Advertisement

The delegate instance itself doesn't belong to a specific module, it is merely referring to a function and/or an object instance. Technically it may even be that the delegate holds a reference to a function from one module and an instance of an object from another module.

I'll update the documentation to mention this.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Thanks. I've redesigned my callback system to store the module that the calling function belongs to since that's the module that the callback was expected to be owned by.

This topic is closed to new replies.

Advertisement