Skip to content

Commit

Permalink
fixed a potential memory leak in the VirtualOCTSystem that can occur …
Browse files Browse the repository at this point in the history
…due to a race condition when restarting the acquisition too quickly
  • Loading branch information
spectralcode committed Mar 2, 2024
1 parent 4726acc commit bf14153
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
MIT License
Copyright (c) 2019-2022 Miroslav Zabic
Copyright (c) 2019-2024 Miroslav Zabic
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -31,6 +31,7 @@ VirtualOCTSystem::VirtualOCTSystem() {
this->name = "Virtual OCT System";
this->file = nullptr;
this->streamBuffer = nullptr;
this->isCleanupPending = false;

connect(this->systemDialog, &VirtualOCTSystemSettingsDialog::settingsUpdated, this, &VirtualOCTSystem::slot_updateParams);
connect(this, &VirtualOCTSystem::enableGui, this->systemDialog, &VirtualOCTSystemSettingsDialog::slot_enableGui);
Expand Down Expand Up @@ -72,19 +73,23 @@ bool VirtualOCTSystem::init() {


void VirtualOCTSystem::startAcquisition(){
//check if cleanup is pending from previous acquisition
if(this->isCleanupPending){
this->cleanup();
}

//init acquisition
bool initSuccessfull = this->init();
if(!initSuccessfull){
emit enableGui(true);
emit info(tr("Initialization unsuccessful. Acquisition stopped."));
this->buffer->releaseMemory();
this->cleanup();
emit acquisitionStopped();
return;
}

//start acquisition
emit info("Acquisition started");
emit info("Acquisition startedd");
if(currParams.buffersFromFile <= 2){
this->acqcuisitionSimulation();
}else if(currParams.copyFileToRam){
Expand All @@ -94,15 +99,16 @@ void VirtualOCTSystem::startAcquisition(){
}

//acuquisition stopped
this->isCleanupPending = true;
emit enableGui(true);
emit info("Acquisistion stopped!");
emit acquisitionStopped();
//wait some time before releasing buffer memory to allow extensions and 1d plot window to process last raw buffer
QCoreApplication::processEvents();
QThread::msleep(500);
QCoreApplication::processEvents();
this->buffer->releaseMemory();
this->cleanup();
this->isCleanupPending = false;
}

void VirtualOCTSystem::stopAcquisition(){
Expand All @@ -112,6 +118,8 @@ void VirtualOCTSystem::stopAcquisition(){
}

void VirtualOCTSystem::cleanup() {
this->buffer->releaseMemory();

if(this->streamBuffer != nullptr){
delete this->streamBuffer;
this->streamBuffer = nullptr;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
MIT License
Copyright (c) 2019-2022 Miroslav Zabic
Copyright (c) 2019-2024 Miroslav Zabic
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -57,6 +57,7 @@ class VirtualOCTSystem : public AcquisitionSystem
VirtualOCTSystemSettingsDialog* systemDialog;
simulatorParams currParams;
AcquisitionBuffer* streamBuffer;
bool isCleanupPending ;

bool init();
void cleanup();
Expand Down

0 comments on commit bf14153

Please sign in to comment.