From 0067e309d18b917298cab8546d11a403517d75bf Mon Sep 17 00:00:00 2001 From: "Charles J. Cliffe" Date: Wed, 8 Jun 2016 21:31:52 -0400 Subject: [PATCH] more debugging; fix some potential refcount leaks... --- src/IOThread.h | 22 +++++++++++++++++++--- src/audio/AudioThread.cpp | 12 ++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/IOThread.h b/src/IOThread.h index 2397018..5a2a1da 100644 --- a/src/IOThread.h +++ b/src/IOThread.h @@ -24,6 +24,16 @@ class ReferenceCounter { public: + void setIndex(int idx) { + std::lock_guard < std::recursive_mutex > lock(m_mutex); + index = idx; + } + + int getIndex() { + std::lock_guard < std::recursive_mutex > lock(m_mutex); + return index; + } + void setRefCount(int rc) { std::lock_guard < std::recursive_mutex > lock(m_mutex); refCount = rc; @@ -49,7 +59,7 @@ protected: mutable std::recursive_mutex m_mutex; private: - int refCount; + int index, refCount; }; @@ -66,7 +76,7 @@ public: garbageRemoval.push_back(*i); } else { - std::cout << "Garbage in queue " << (*i)->getRefCount() << " usage(s)" << std::endl; + std::cout << "Garbage in queue buffer idx #" << (*i)->getIndex() << ", " << (*i)->getRefCount() << " usage(s)" << std::endl; } } if ( garbageRemoval.size() ) { @@ -96,7 +106,7 @@ class ReBuffer { public: ReBuffer(std::string bufferId) : bufferId(bufferId) { - + indexCounter.store(0); } BufferType *getBuffer() { @@ -118,6 +128,7 @@ public: outputBuffers.pop_back(); delete ref; } + buf->setIndex(indexCounter++); return buf; } @@ -128,6 +139,7 @@ public: buf = new BufferType(); buf->setRefCount(1); + buf->setIndex(indexCounter++); outputBuffers.push_back(buf); return buf; @@ -135,6 +147,9 @@ public: void purge() { std::lock_guard < std::mutex > lock(m_mutex); + if (bufferId == "DemodulatorThreadBuffers") { + std::cout << "'" << bufferId << "' purging.. total indexes: " << indexCounter.load() << std::endl; + } while (!outputBuffers.empty()) { BufferType *ref = outputBuffers.front(); outputBuffers.pop_front(); @@ -153,6 +168,7 @@ public: std::deque outputBuffers; typename std::deque::iterator outputBuffersI; mutable std::mutex m_mutex; + std::atomic_int indexCounter; }; diff --git a/src/audio/AudioThread.cpp b/src/audio/AudioThread.cpp index bc51ef7..7b18336 100644 --- a/src/audio/AudioThread.cpp +++ b/src/audio/AudioThread.cpp @@ -84,6 +84,9 @@ static int audioCallback(void *outputBuffer, void * /* inputBuffer */, unsigned } srcmix->inputQueue->pop(srcmix->currentInput); if (srcmix->isTerminated()) { + if (srcmix->currentInput) { + srcmix->currentInput->decRefCount(); + } continue; } continue; @@ -123,6 +126,9 @@ static int audioCallback(void *outputBuffer, void * /* inputBuffer */, unsigned } srcmix->inputQueue->pop(srcmix->currentInput); if (srcmix->isTerminated()) { + if (srcmix->currentInput) { + srcmix->currentInput->decRefCount(); + } continue; } } @@ -144,6 +150,9 @@ static int audioCallback(void *outputBuffer, void * /* inputBuffer */, unsigned } srcmix->inputQueue->pop(srcmix->currentInput); if (srcmix->isTerminated()) { + if (srcmix->currentInput) { + srcmix->currentInput->decRefCount(); + } break; } float srcPeak = srcmix->currentInput->peak * srcmix->gain; @@ -171,6 +180,9 @@ static int audioCallback(void *outputBuffer, void * /* inputBuffer */, unsigned } srcmix->inputQueue->pop(srcmix->currentInput); if (srcmix->isTerminated()) { + if (srcmix->currentInput) { + srcmix->currentInput->decRefCount(); + } break; } float srcPeak = srcmix->currentInput->peak * srcmix->gain;