소스 검색

fixed some memory leaks

Kolja Strohm 1 일 전
부모
커밋
14cc773e03
6개의 변경된 파일35개의 추가작업 그리고 13개의 파일을 삭제
  1. 8 3
      Assembly.cpp
  2. 10 4
      Console.cpp
  3. 9 3
      Console.h
  4. 1 1
      DataValidator.cpp
  5. 2 1
      Global.cpp
  6. 5 1
      JSON.cpp

+ 8 - 3
Assembly.cpp

@@ -1,5 +1,6 @@
 #include "Assembly.h"
 
+#include "Critical.h"
 #include "InMemoryBuffer.h"
 
 #ifndef WIN32
@@ -726,8 +727,8 @@ public:
 class OperationCodeTable : public Framework::ReferenceCounter
 {
 public:
-    thread_local static Framework::RCArray<OperationCodeTable>
-        machineCodeTranslationTable;
+    static Framework::RCArray<OperationCodeTable> machineCodeTranslationTable;
+    static Framework::Critical machineCodeTranslationTableLock;
 
 private:
     Framework::Assembly::Operation op;
@@ -1485,9 +1486,11 @@ public:
     }
 };
 
-thread_local Framework::RCArray<OperationCodeTable>
+Framework::RCArray<OperationCodeTable>
     OperationCodeTable::machineCodeTranslationTable;
 
+Framework::Critical OperationCodeTable::machineCodeTranslationTableLock;
+
 std::function<bool(const Framework::Assembly::OperationArgument& arg)>
 isGPRegister(Framework::Assembly::MemoryBlockSize size)
 {
@@ -1589,6 +1592,7 @@ isFPRegisterOrMEmoryAccess(Framework::Assembly::MemoryBlockSize regSize,
 
 void __intializeMachineCodeTranslationTable()
 {
+    OperationCodeTable::machineCodeTranslationTableLock.lock();
     if (!OperationCodeTable::machineCodeTranslationTable.getEntryCount())
     {
         OperationCodeTable::machineCodeTranslationTable.add(
@@ -6667,6 +6671,7 @@ void __intializeMachineCodeTranslationTable()
                         READ),
                 }));
     }
+    OperationCodeTable::machineCodeTranslationTableLock.unlock();
 }
 
 bool Framework::Assembly::OperationArgument::usesRegister(GPRegister reg) const

+ 10 - 4
Console.cpp

@@ -419,12 +419,13 @@ Framework::InputLine::InputLine()
       Thread(),
       cursorPos(0),
       input(""),
-      suggestions(0)
+      suggestions(0),
+      closed(false)
 {}
 
 Framework::InputLine::~InputLine()
 {
-    suggestions->release();
+    if (suggestions) suggestions->release();
 }
 
 void Framework::InputLine::addPossibleCommand(ConsoleCommand* command)
@@ -437,6 +438,11 @@ bool Framework::InputLine::isInput()
     return true;
 }
 
+void Framework::InputLine::close()
+{
+    closed = true;
+}
+
 void Framework::InputLine::setCursorToBeginning()
 {
     cs.lock();
@@ -470,7 +476,7 @@ void Framework::InputLine::thread()
     INPUT_RECORD inputRecord;
     DWORD eventsRead;
     HANDLE handle = GetStdHandle(STD_INPUT_HANDLE);
-    while (true)
+    while (!closed)
     {
         if (ReadConsoleInput(handle, &inputRecord, 1, &eventsRead))
         {
@@ -563,7 +569,7 @@ void Framework::InputLine::thread()
     }
 #else
     char c;
-    while (read(STDIN_FILENO, &c, 1) == 1)
+    while (!closed && read(STDIN_FILENO, &c, 1) == 1)
     {
         if (c == 27)
         { // Check for the escape key (27 is the ASCII code for escape)

+ 9 - 3
Console.h

@@ -1,4 +1,4 @@
-#pragma once
+#pragma once
 
 #include "Array.h"
 #include "Critical.h"
@@ -66,8 +66,7 @@ namespace Framework
          */
         virtual void addAutocompletePossibilities(const RCArray<Text>& args,
             bool appendToLast,
-            RCArray<Text>& possibilities) const
-            = 0;
+            RCArray<Text>& possibilities) const = 0;
         /**
          * executes the command
          *
@@ -347,6 +346,7 @@ namespace Framework
         int cursorPos;
         RCArray<ConsoleCommand> commands;
         Critical cs;
+        bool closed;
 
     protected:
         Framework::Text input;
@@ -368,6 +368,12 @@ namespace Framework
          * \return true
          */
         DLLEXPORT virtual bool isInput() override;
+        /**
+         * stops the input line thread and prevents further user input
+         * this will not abort the current read operation. The thread will only
+         * exit correctly when called during command execution
+         */
+        DLLEXPORT void close();
 
     protected:
         /**

+ 1 - 1
DataValidator.cpp

@@ -1470,7 +1470,7 @@ JSON::JSONObject* Framework::Validator::DataValidator::getJsonSchema(
         if (!zDefs->hasValue(id))
         {
             JSON::JSONObject* def = getJsonSchema(e, zDefs);
-            if (!def->hasValue(id))
+            if (!zDefs->hasValue(id))
             {
                 zDefs->addValue(id, def);
             }

+ 2 - 1
Global.cpp

@@ -53,10 +53,11 @@ void Framework::initFramework(HINSTANCE__* hInst)
 void Framework::releaseFramework()
 {
     if (!isInitialized) return;
+    loggingHandler->release(); // must be cleaned up before the thread register
+                               // because it may reference threads
     thRegister->cleanUpClosedThreads();
     dlls->release();
     delete thRegister;
-    loggingHandler->release();
     isInitialized = 0;
 }
 

+ 5 - 1
JSON.cpp

@@ -359,7 +359,11 @@ JSONObject& JSONObject::operator=(const JSONObject& obj)
 
 bool JSONObject::addValue(Text field, JSONValue* value)
 {
-    if (hasValue(field)) return 0;
+    if (hasValue(field))
+    {
+        value->release();
+        return 0;
+    }
     fields->add(field);
     values->add(value);
     return 1;