Browse Source

crash fix on exit

David Rose 16 years ago
parent
commit
029eb75f78

+ 28 - 13
direct/src/plugin_activex/PPInstance.cpp

@@ -428,7 +428,7 @@ int PPInstance::LoadPlugin( const std::string& dllFilename )
 {
 {
     if ( !m_pluginLoaded )
     if ( !m_pluginLoaded )
     { 
     { 
-        s_instanceCount += 1;
+        ref_plugin();
         m_pluginLoaded = true;
         m_pluginLoaded = true;
     }
     }
 
 
@@ -479,22 +479,37 @@ int PPInstance::UnloadPlugin()
     if ( m_pluginLoaded )
     if ( m_pluginLoaded )
     { 
     { 
         m_pluginLoaded = false;
         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;
     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  )
 int PPInstance::Start( const std::string& p3dFilename  )
 {
 {
     m_eventStop.ResetEvent();
     m_eventStop.ResetEvent();

+ 3 - 0
direct/src/plugin_activex/PPInstance.h

@@ -50,6 +50,9 @@ public:
     int LoadPlugin( const std::string& dllFilename );
     int LoadPlugin( const std::string& dllFilename );
     int UnloadPlugin( void );
     int UnloadPlugin( void );
 
 
+    static void ref_plugin();
+    static void unref_plugin();
+
     int Start( const std::string& p3dFileName );
     int Start( const std::string& p3dFileName );
 
 
     std::string GetHostUrl( );
     std::string GetHostUrl( );

+ 5 - 1
direct/src/plugin_activex/PPPandaObject.cpp

@@ -14,6 +14,7 @@
 
 
 #include "stdafx.h"
 #include "stdafx.h"
 #include "PPPandaObject.h"
 #include "PPPandaObject.h"
+#include "PPInstance.h"
 #include "load_plugin.h"
 #include "load_plugin.h"
 
 
 PPandaObject::PPandaObject( PPInterface* interfac, P3D_object* p3dObject ) : 
 PPandaObject::PPandaObject( PPInterface* interfac, P3D_object* p3dObject ) : 
@@ -24,6 +25,7 @@ PPandaObject::PPandaObject( PPInterface* interfac, P3D_object* p3dObject ) :
         P3D_OBJECT_INCREF( m_p3dObject );
         P3D_OBJECT_INCREF( m_p3dObject );
     }
     }
     AddRef();
     AddRef();
+    PPInstance::ref_plugin();
 }
 }
 
 
 PPandaObject::~PPandaObject()
 PPandaObject::~PPandaObject()
@@ -35,10 +37,12 @@ PPandaObject::~PPandaObject()
 
 
     // Clean up the p3d_object, but only if we haven't already
     // Clean up the p3d_object, but only if we haven't already
     // unloaded the plugin.
     // unloaded the plugin.
-    if ( m_p3dObject && is_plugin_loaded() )
+    if ( m_p3dObject )
     {
     {
         P3D_OBJECT_DECREF( m_p3dObject );
         P3D_OBJECT_DECREF( m_p3dObject );
     }
     }
+
+    PPInstance::unref_plugin();
 }
 }
 
 
 // Dispatch Methods
 // Dispatch Methods