From 7fa0d74c46fae50316031fb36baa91dc764821b5 Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Mon, 15 Aug 2005 01:02:27 +0000 Subject: [PATCH] A memory leak fix --- afeed.h | 1 + afeedr.h | 2 ++ player.h | 1 + playerradio.h | 1 + playervideo.cc | 24 ++++++++++++++++++------ playervideo.h | 2 ++ thread.cc | 2 ++ thread.h | 3 +++ vconnect.h | 1 + vfeed.h | 1 + 10 files changed, 32 insertions(+), 6 deletions(-) diff --git a/afeed.h b/afeed.h index b858e96..ba465c9 100644 --- a/afeed.h +++ b/afeed.h @@ -44,6 +44,7 @@ class AFeed : public Thread private: void threadMethod(); + void threadPostStopCleanup() {}; int audioEnabled; int fd; Callback& cb; diff --git a/afeedr.h b/afeedr.h index eb94db2..939d919 100644 --- a/afeedr.h +++ b/afeedr.h @@ -42,6 +42,8 @@ class AFeedR : public Thread private: void threadMethod(); + void threadPostStopCleanup() {}; + int fd; Callback& cb; Stream& stream; diff --git a/player.h b/player.h index 65bf70f..87711ce 100644 --- a/player.h +++ b/player.h @@ -49,6 +49,7 @@ class Player : public Thread, public Callback virtual void call()=0; // for callback interface virtual void threadMethod()=0; // for thread interface + virtual void threadPostStopCleanup()=0; protected: int initted; diff --git a/playerradio.h b/playerradio.h index 82a9ce0..dab5d3e 100644 --- a/playerradio.h +++ b/playerradio.h @@ -54,6 +54,7 @@ class PlayerRadio : public Player void setLength(ULLONG length); void threadMethod(); + void threadPostStopCleanup() {}; void skipForward(int) {}; void skipBackward(int) {}; diff --git a/playervideo.cc b/playervideo.cc index 8b4fe5e..4ad52fe 100644 --- a/playervideo.cc +++ b/playervideo.cc @@ -33,6 +33,7 @@ PlayerVideo::PlayerVideo(MessageQueue* messageQueue, UCHAR tIsRecording) feedMode = MODE_NORMAL; isRecording = tIsRecording; lastRescan = 0; + threadBuffer = NULL; } PlayerVideo::~PlayerVideo() @@ -491,7 +492,6 @@ void PlayerVideo::call() void PlayerVideo::threadMethod() { - UCHAR* buf; UINT thisRead; UINT writeLength; UINT thisWrite; @@ -542,12 +542,12 @@ void PlayerVideo::threadMethod() askFor = blockSize; // normal } - buf = vdr->getBlock(feedPosition, askFor, &thisRead); - if (!buf) break; + threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead); + if (!threadBuffer) break; if (startup) { - int a_stream = demuxer.scan(buf, thisRead); + int a_stream = demuxer.scan(threadBuffer, thisRead); demuxer.setAudioStream(a_stream); Log::getInstance()->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream); startup = 0; @@ -575,7 +575,7 @@ void PlayerVideo::threadMethod() while(writeLength < thisRead) { - thisWrite = demuxer.put(buf + writeLength, thisRead - writeLength); + thisWrite = demuxer.put(threadBuffer + writeLength, thisRead - writeLength); writeLength += thisWrite; if (!thisWrite) @@ -589,7 +589,8 @@ void PlayerVideo::threadMethod() threadCheckExit(); } - free(buf); + free(threadBuffer); + threadBuffer = NULL; } @@ -603,3 +604,14 @@ void PlayerVideo::threadMethod() } + +void PlayerVideo::threadPostStopCleanup() +{ + Log::getInstance()->log("Player", Log::DEBUG, "Post stop cleanup 1"); + if (threadBuffer) + { + Log::getInstance()->log("Player", Log::DEBUG, "Post stop cleanup 2"); + free(threadBuffer); + threadBuffer = NULL; + } +} diff --git a/playervideo.h b/playervideo.h index eb58849..96ad61c 100644 --- a/playervideo.h +++ b/playervideo.h @@ -60,6 +60,7 @@ class PlayerVideo : public Player void setLength(ULLONG length); void threadMethod(); + void threadPostStopCleanup(); private: MessageQueue* commandMessageQueue; @@ -76,6 +77,7 @@ class PlayerVideo : public Player const static UCHAR MODE_BACKWARDS = 2; const static UINT blockSize = 100000; const static UINT startupBlockSize = 250000; + UCHAR* threadBuffer; UCHAR playing; // As in not stopped, (playing && paused) can == TRUE UCHAR paused; // Must be in playing state as well diff --git a/thread.cc b/thread.cc index 75b4281..b6cc8ff 100644 --- a/thread.cc +++ b/thread.cc @@ -53,6 +53,7 @@ void Thread::threadStop() // Signal thread here in case it's waiting threadSignal(); pthread_join(pthread, NULL); + this->threadPostStopCleanup(); } void Thread::threadCancel() @@ -60,6 +61,7 @@ void Thread::threadCancel() threadActive = 0; pthread_cancel(pthread); pthread_join(pthread, NULL); + this->threadPostStopCleanup(); } void Thread::threadCheckExit() diff --git a/thread.h b/thread.h index d57375a..111ede4 100644 --- a/thread.h +++ b/thread.h @@ -24,11 +24,14 @@ #include #include +#include // temp + class Thread { protected: // Override this method in derived classes virtual void threadMethod()=0; + virtual void threadPostStopCleanup()=0; // Methods to use from outside the thread int threadStart(); // start the thread. threadMethod() will be called in derived class diff --git a/vconnect.h b/vconnect.h index 545f5a9..b9f086e 100644 --- a/vconnect.h +++ b/vconnect.h @@ -48,6 +48,7 @@ class VConnect : public VInfo, public Thread private: void threadMethod(); + void threadPostStopCleanup() {}; void clearServerIPs(); UCHAR irun; diff --git a/vfeed.h b/vfeed.h index 2e7019b..51cd666 100644 --- a/vfeed.h +++ b/vfeed.h @@ -42,6 +42,7 @@ class VFeed : public Thread private: void threadMethod(); + void threadPostStopCleanup() {}; int fd; Callback& cb; struct timespec delayTime; -- 2.39.5