Skip to content

Commit

Permalink
Fix crash with more than one audio stream
Browse files Browse the repository at this point in the history
- Ensure thread syncronisation
- Use free instead of delete[] for strdup'ed data

Signed-off-by: Maxime Gervais <[email protected]>
  • Loading branch information
g-maxime committed Dec 11, 2024
1 parent 3d1cfd0 commit 693ec1a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
23 changes: 21 additions & 2 deletions Source/Core/CommonStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ extern "C"

#include "Core/Core.h"
#include "tinyxml2.h"
#include <QMutexLocker>
#include <sstream>
#include <iomanip>
#include <cstdlib>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions Source/Core/CommonStats.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <cctype>
#include <functional>
#include <Core/Core.h>
#include <QMutex>
#include <QtAVPlayer/qavplayer.h>

struct AVFrame;
Expand Down Expand Up @@ -198,6 +199,9 @@ class CommonStats
int** additionalIntStats;
double** additionalDoubleStats;
char*** additionalStringStats;

// Thread synchronisation
QMutex Mutex;
};

#endif // Stats_H

0 comments on commit 693ec1a

Please sign in to comment.