From fb839736cfb1399d37a5e55edb685f4fe72ae471 Mon Sep 17 00:00:00 2001 From: 30hours Date: Wed, 11 Sep 2024 14:02:39 +0000 Subject: [PATCH] Fix double free error by moving mutex lock --- src/blah2.cpp | 24 +++++++++++++++--------- src/data/IqData.cpp | 4 +++- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/blah2.cpp b/src/blah2.cpp index 24fb81f..70055de 100644 --- a/src/blah2.cpp +++ b/src/blah2.cpp @@ -86,10 +86,11 @@ int main(int argc, char **argv) } // create shared queue - double tBuffer; + double tCpi, tBuffer; + tree["process"]["data"]["cpi"] >> tCpi; tree["process"]["data"]["buffer"] >> tBuffer; - IqData *buffer1 = new IqData((int) (tBuffer*fs)); - IqData *buffer2 = new IqData((int) (tBuffer*fs)); + IqData *buffer1 = new IqData((int) (tCpi*tBuffer*fs)); + IqData *buffer2 = new IqData((int) (tCpi*tBuffer*fs)); // run capture std::thread t1([&]{capture->process(buffer1, buffer2, @@ -97,8 +98,6 @@ int main(int argc, char **argv) }); // setup process CPI - double tCpi; - tree["process"]["data"]["cpi"] >> tCpi; uint32_t nSamples = fs * tCpi; IqData *x = new IqData(nSamples); IqData *y = new IqData(nSamples); @@ -227,17 +226,16 @@ int main(int argc, char **argv) std::thread t2([&]{ while (true) { + buffer1->lock(); + buffer2->lock(); if ((buffer1->get_length() > nSamples) && (buffer2->get_length() > nSamples)) { time.push_back(current_time_us()); - // extract data from buffer - buffer1->lock(); - buffer2->lock(); for (uint32_t i = 0; i < nSamples; i++) { x->push_back(buffer1->pop_front()); - y->push_back(buffer2->pop_front()); + y->push_back(buffer2->pop_front()); } buffer1->unlock(); buffer2->unlock(); @@ -327,6 +325,14 @@ int main(int argc, char **argv) std::string t0_string = std::to_string(time[0]/1000); socket_timestamp.sendData(t0_string); time.clear(); + + } + else + { + buffer1->unlock(); + buffer2->unlock(); + // short delay to prevent tight looping + std::this_thread::sleep_for(std::chrono::milliseconds(1)); } } }); diff --git a/src/data/IqData.cpp b/src/data/IqData.cpp index 5373ec8..2c9bb81 100644 --- a/src/data/IqData.cpp +++ b/src/data/IqData.cpp @@ -54,11 +54,13 @@ void IqData::push_back(std::complex sample) std::complex IqData::pop_front() { + if (data->empty()) { + throw std::runtime_error("Attempting to pop from an empty deque"); + } std::complex sample = data->front(); data->pop_front(); return sample; } - void IqData::print() { int n = data->size();