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