diff --git a/direct/src/plugin_activex/PPInstance.cpp b/direct/src/plugin_activex/PPInstance.cpp index 189cfeaed0..c96399eca5 100644 --- a/direct/src/plugin_activex/PPInstance.cpp +++ b/direct/src/plugin_activex/PPInstance.cpp @@ -428,7 +428,7 @@ int PPInstance::LoadPlugin( const std::string& dllFilename ) { if ( !m_pluginLoaded ) { - s_instanceCount += 1; + ref_plugin(); m_pluginLoaded = true; } @@ -479,22 +479,37 @@ int PPInstance::UnloadPlugin() if ( m_pluginLoaded ) { m_pluginLoaded = false; - assert( s_instanceCount > 0 ); - s_instanceCount -= 1; - - if ( s_instanceCount == 0 && is_plugin_loaded() ) - { - unload_plugin(); - m_isInit = false; - - // This pointer is no longer valid and must be reset for next - // time. - PPBrowserObject::clear_class_definition(); - } + m_isInit = false; + unref_plugin(); } return error; } +// Increments the reference count on the "plugin" library (i.e. the +// core API). Call unref_plugin() later to decrement this count. +void PPInstance:: +ref_plugin() { + s_instanceCount += 1; +} + +// Decrements the reference count on the "plugin" library. This must +// correspond to an earlier call to ref_plugin(). When the last +// reference is removed, the plugin will be unloaded. +void PPInstance:: +unref_plugin() { + assert( s_instanceCount > 0 ); + s_instanceCount -= 1; + + if ( s_instanceCount == 0 && is_plugin_loaded() ) { + nout << "Unloading core API\n"; + unload_plugin(); + + // This pointer is no longer valid and must be reset for next + // time. + PPBrowserObject::clear_class_definition(); + } +} + int PPInstance::Start( const std::string& p3dFilename ) { m_eventStop.ResetEvent(); diff --git a/direct/src/plugin_activex/PPInstance.h b/direct/src/plugin_activex/PPInstance.h index 3e31ed2e77..7bc1405234 100644 --- a/direct/src/plugin_activex/PPInstance.h +++ b/direct/src/plugin_activex/PPInstance.h @@ -50,6 +50,9 @@ public: int LoadPlugin( const std::string& dllFilename ); int UnloadPlugin( void ); + static void ref_plugin(); + static void unref_plugin(); + int Start( const std::string& p3dFileName ); std::string GetHostUrl( ); diff --git a/direct/src/plugin_activex/PPPandaObject.cpp b/direct/src/plugin_activex/PPPandaObject.cpp index f8432e0556..636870d60a 100644 --- a/direct/src/plugin_activex/PPPandaObject.cpp +++ b/direct/src/plugin_activex/PPPandaObject.cpp @@ -14,6 +14,7 @@ #include "stdafx.h" #include "PPPandaObject.h" +#include "PPInstance.h" #include "load_plugin.h" PPandaObject::PPandaObject( PPInterface* interfac, P3D_object* p3dObject ) : @@ -24,6 +25,7 @@ PPandaObject::PPandaObject( PPInterface* interfac, P3D_object* p3dObject ) : P3D_OBJECT_INCREF( m_p3dObject ); } AddRef(); + PPInstance::ref_plugin(); } PPandaObject::~PPandaObject() @@ -35,10 +37,12 @@ PPandaObject::~PPandaObject() // Clean up the p3d_object, but only if we haven't already // unloaded the plugin. - if ( m_p3dObject && is_plugin_loaded() ) + if ( m_p3dObject ) { P3D_OBJECT_DECREF( m_p3dObject ); } + + PPInstance::unref_plugin(); } // Dispatch Methods