diff -r fd3ff8d792ce -r e6394d70ced0 core/sourcehook/sourcehook.cpp --- a/core/sourcehook/sourcehook.cpp Sat Oct 15 22:16:50 2011 -0700 +++ b/core/sourcehook/sourcehook.cpp Mon Dec 12 15:53:56 2011 -0600 @@ -1,4 +1,5 @@ /* ======== SourceHook ======== +* vim: set ts=4 sw=4 tw=99 noet: * Copyright (C) 2004-2010 Metamod:Source Development Team * No warranties of any kind * @@ -357,7 +358,7 @@ return m_ContextStack.front().pOverrideRet; } - void CSourceHookImpl::UnloadPlugin(Plugin plug) + void CSourceHookImpl::UnloadPlugin(Plugin plug, UnloadListener *listener) { // 1) Remove all hooks by this plugin @@ -375,6 +376,20 @@ else ++iter; } + + // Fix for bug 5034. It's pretty tricky to find whether hookmanagers owned by this + // plugin are active. We could change how hook managers are tracked in SH, and lazily + // free them as the context stack drops to 0, or we could change the pubfunc API to + // know whether it's active or not. Rather than deal with this extra complexity, we + // just conservatively wait until the context stack hits 0 before unloading. + if (m_ContextStack.size() == 0) + { + listener->ReadyToUnload(plug); + } + else + { + m_PendingUnloads.push_back(new PendingUnload(listener, plug)); + } } CHookManList::iterator CSourceHookImpl::RemoveHookManager(CHookManList::iterator hookman_iter) @@ -571,12 +586,41 @@ return pCtx; } + void CSourceHookImpl::ResolvePendingUnloads(bool force) + { + List::iterator iter = m_PendingUnloads.begin(); + while (iter != m_PendingUnloads.end()) + { + PendingUnload *unload = *iter; + + if (!force && !unload->deactivated()) + { + // Unless being forced, wait one drop of the context stack + // before actually unloading. Otherwise, we'll still return + // to unloaded memory. + unload->deactivate(); + iter++; + } + else + { + unload->listener()->ReadyToUnload(unload->plugin()); + delete unload; + iter = m_PendingUnloads.erase(iter); + } + } + } + void CSourceHookImpl::EndContext(IHookContext *pCtx) { // Do clean up task, if any is associated with this context m_ContextStack.front().DoCleanupTaskAndDeleteIt(); // Then remove it m_ContextStack.pop(); + + // If we've reached 0 contexts and there are pending unloads, + // resolve them now. + if (m_ContextStack.size() == 0 && m_PendingUnloads.size() != 0) + ResolvePendingUnloads(); } void CSourceHookImpl::CompleteShutdown()