diff --git a/Source/Core/CommonStats.cpp b/Source/Core/CommonStats.cpp index 7855dc38e..c1e0cc196 100644 --- a/Source/Core/CommonStats.cpp +++ b/Source/Core/CommonStats.cpp @@ -19,6 +19,7 @@ extern "C" #include "Core/Core.h" #include "tinyxml2.h" +#include #include #include #include @@ -126,6 +127,9 @@ CommonStats::CommonStats (const struct per_item* PerItem_, int Type_, size_t Cou //--------------------------------------------------------------------------- CommonStats::~CommonStats() { + //Lock data + QMutexLocker Lock(&Mutex); + // Data - Counts delete[] Stats_Totals; delete[] Stats_Counts; @@ -154,9 +158,9 @@ CommonStats::~CommonStats() delete[] pict_type_char; for (size_t j = 0; j < Data_Reserved; ++j) - delete [] comments[j]; + free(comments[j]); - delete [] comments; + delete[] comments; auto numberOfIntValues = lastStatsIndexByValueType[StatsValueInfo::Int]; @@ -189,6 +193,9 @@ void CommonStats::processAdditionalStats(const char* key, const char* value, boo if (strcmp(key, "qctools.comment") == 0) return; + // Lock data + QMutexLocker Lock(&Mutex); + if(!statsMapInitialized) { auto type = StatsValueInfo::typeFromKey(key, value); auto stats = StatsValueInfo { @@ -223,6 +230,9 @@ void CommonStats::processAdditionalStats(const char* key, const char* value, boo void CommonStats::writeAdditionalStats(std::stringstream &stream, size_t index) { + // Lock data + QMutexLocker Lock(&Mutex); + if(additionalIntStats) { for(size_t i = 0; i < statsKeysByIndexByValueType[StatsValueInfo::Int].size(); ++i) { auto key = statsKeysByIndexByValueType[StatsValueInfo::Int][i]; @@ -253,6 +263,9 @@ void CommonStats::writeAdditionalStats(std::stringstream &stream, size_t index) void CommonStats::updateAdditionalStats(StatsValueInfo::Type type, size_t oldSize, size_t size) { + // Lock data + QMutexLocker Lock(&Mutex); + if (type==StatsValueInfo::Int) { auto additionalIntStats_Old = additionalIntStats; @@ -305,6 +318,9 @@ void CommonStats::updateAdditionalStats(StatsValueInfo::Type type, size_t oldSiz void CommonStats::initializeAdditionalStats() { + // Lock data + QMutexLocker Lock(&Mutex); + auto numberOfIntValues = lastStatsIndexByValueType[StatsValueInfo::Int]; if(numberOfIntValues != 0) { additionalIntStats = new int*[numberOfIntValues]; @@ -517,6 +533,9 @@ void CommonStats::statsFromExternalData(const char *Data, size_t Size, const std //--------------------------------------------------------------------------- void CommonStats::Data_Reserve(size_t NewValue) { + // Lock data + QMutexLocker Lock(&Mutex); + // Saving old data size_t Data_Reserved_Old = Data_Reserved; double** x_Old = x; diff --git a/Source/Core/CommonStats.h b/Source/Core/CommonStats.h index c7b669ee9..b97902569 100644 --- a/Source/Core/CommonStats.h +++ b/Source/Core/CommonStats.h @@ -17,6 +17,7 @@ #include #include #include +#include #include struct AVFrame; @@ -198,6 +199,9 @@ class CommonStats int** additionalIntStats; double** additionalDoubleStats; char*** additionalStringStats; + + // Thread synchronisation + QMutex Mutex; }; #endif // Stats_H