Browse Source

Guard LuaScript subsystem against unsubscribing internally listened events.

Lasse Öörni 11 years ago
parent
commit
8f2ed8b97b
2 changed files with 10 additions and 4 deletions
  1. 7 3
      Source/Engine/LuaScript/LuaScript.cpp
  2. 3 1
      Source/Engine/LuaScript/LuaScript.h

+ 7 - 3
Source/Engine/LuaScript/LuaScript.cpp

@@ -111,6 +111,10 @@ LuaScript::LuaScript(Context* context) :
 
     // Subscribe to console commands
     SubscribeToEvent(E_CONSOLECOMMAND, HANDLER(LuaScript, HandleConsoleCommand));
+    
+    // Record the internally handled script functions so that UnsubscribeFromAllEvents doesn't destroy them
+    internalEvents_.Push(E_POSTUPDATE);
+    internalEvents_.Push(E_CONSOLECOMMAND);
 }
 
 LuaScript::~LuaScript()
@@ -196,7 +200,7 @@ void LuaScript::ScriptUnsubscribeFromEvent(const String& eventName, const String
         {
             UnsubscribeFromEvent(eventType);
             eventHandleFunctions_.Erase(i);
-        }   
+        }
     }
 }
 
@@ -204,8 +208,8 @@ void LuaScript::ScriptUnsubscribeFromAllEvents()
 {
     if (eventHandleFunctions_.Empty())
         return;
-
-    UnsubscribeFromAllEvents();
+    
+    UnsubscribeFromAllEventsExcept(internalEvents_, false);
 
     eventHandleFunctions_.Clear();
 }

+ 3 - 1
Source/Engine/LuaScript/LuaScript.h

@@ -109,8 +109,10 @@ private:
     typedef Vector<WeakPtr<LuaFunction> > LuaFunctionVector;
     /// Event handle functions.
     HashMap<StringHash, LuaFunctionVector> eventHandleFunctions_;
-    /// Object event handle funcitons.
+    /// Object event handle functions.
     HashMap<Object*, HashMap<StringHash, LuaFunctionVector> > objectHandleFunctions_;
+    /// Internally used events, which should not be unsubscribed from.
+    PODVector<StringHash> internalEvents_;
     /// Flag for executing engine console commands as script code.
     bool executeConsoleCommands_;
 };