Skip to content

Commit 8268ef2

Browse files
author
Hugh Sanderson
committed
Fix memory leaks in objects associated with thread creation. For #1223
1 parent 86d4b1a commit 8268ef2

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

include/hx/Thread.h

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ inline bool HxCreateDetachedThread(DWORD (WINAPI *func)(void *), void *param)
8686

8787
struct HxMutex
8888
{
89+
bool mValid;
90+
pthread_mutex_t *mMutex;
91+
8992
HxMutex()
9093
{
9194
pthread_mutexattr_t mta;
@@ -96,9 +99,7 @@ struct HxMutex
9699
}
97100
~HxMutex()
98101
{
99-
if (mValid)
100-
pthread_mutex_destroy(mMutex);
101-
delete mMutex;
102+
Clean();
102103
}
103104
void Lock() { pthread_mutex_lock(mMutex); }
104105
void Unlock() { pthread_mutex_unlock(mMutex); }
@@ -107,12 +108,16 @@ struct HxMutex
107108
void Clean()
108109
{
109110
if (mValid)
111+
{
110112
pthread_mutex_destroy(mMutex);
111-
mValid = 0;
113+
mValid = false;
114+
}
115+
if (mMutex)
116+
{
117+
delete mMutex;
118+
mMutex = nullptr;
119+
}
112120
}
113-
114-
bool mValid;
115-
pthread_mutex_t *mMutex;
116121
};
117122

118123
#define THREAD_FUNC_TYPE void *
@@ -198,19 +203,20 @@ struct HxSemaphore
198203

199204
struct HxSemaphore
200205
{
206+
HxMutex mMutex;
207+
pthread_cond_t *mCondition;
208+
bool mSet;
209+
210+
201211
HxSemaphore()
202212
{
203213
mSet = false;
204-
mValid = true;
205214
mCondition = new pthread_cond_t();
206215
pthread_cond_init(mCondition,0);
207216
}
208217
~HxSemaphore()
209218
{
210-
if (mValid)
211-
{
212-
pthread_cond_destroy(mCondition);
213-
}
219+
Clean();
214220
}
215221
// For autolock
216222
inline operator HxMutex &() { return mMutex; }
@@ -294,19 +300,14 @@ struct HxSemaphore
294300
void Clean()
295301
{
296302
mMutex.Clean();
297-
if (mValid)
303+
if (mCondition)
298304
{
299-
mValid = false;
300305
pthread_cond_destroy(mCondition);
306+
delete mCondition;
307+
mCondition = nullptr;
301308
}
302-
delete mCondition;
303309
}
304310

305-
306-
HxMutex mMutex;
307-
pthread_cond_t *mCondition;
308-
bool mSet;
309-
bool mValid;
310311
};
311312

312313

0 commit comments

Comments
 (0)