From 59a1091ef770a6cbac2f184b0e5b3ac47dd5eff1 Mon Sep 17 00:00:00 2001 From: Maxime Gervais Date: Wed, 11 Dec 2024 14:03:44 +0100 Subject: [PATCH] Fix crash with more than one audio stream - Ensure thread syncronisation - Use free instead of delete[] for strdup'ed data Signed-off-by: Maxime Gervais --- Source/Core/CommonStats.cpp | 26 ++++++++++++++++++++++++-- Source/Core/CommonStats.h | 4 ++++ Source/Core/FileInformation.cpp | 4 ++-- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Source/Core/CommonStats.cpp b/Source/Core/CommonStats.cpp index 7855dc38e..c91266977 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]; @@ -367,6 +383,9 @@ double CommonStats::State_Get() //--------------------------------------------------------------------------- void CommonStats::StatsFinish () { + // Lock data + QMutexLocker Lock(&Mutex); + // Adaptation if (x_Current==1) { @@ -517,6 +536,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 diff --git a/Source/Core/FileInformation.cpp b/Source/Core/FileInformation.cpp index 36d86e9ef..54f707cd2 100644 --- a/Source/Core/FileInformation.cpp +++ b/Source/Core/FileInformation.cpp @@ -1060,7 +1060,7 @@ FileInformation::FileInformation (SignalServer* signalServer, const QString &Fil QObject::connect(m_mediaParser, &QAVPlayer::audioFrame, m_mediaParser, [this](const QAVAudioFrame &frame) { qDebug() << "audio frame came from: " << frame.filterName() << frame.stream() << frame.stream().index(); - if (frame.filterName() == astats) { + if (frame.filterName() == astats && frame.stream().index() < Stats.size()) { auto stat = Stats[frame.stream().index()]; stat->TimeStampFromFrame(frame, stat->x_Current); @@ -1074,7 +1074,7 @@ FileInformation::FileInformation (SignalServer* signalServer, const QString &Fil QObject::connect(m_mediaParser, &QAVPlayer::videoFrame, m_mediaParser, [this](const QAVVideoFrame &frame) { qDebug() << "video frame came from: " << frame.filterName() << frame.stream() << frame.stream().index(); - if(frame.filterName() == stats) { + if(frame.filterName() == stats && frame.stream().index() < Stats.size()) { auto stat = Stats[frame.stream().index()]; stat->TimeStampFromFrame(frame, stat->x_Current);