cwThread.cpp : Added call to pthread_attr_destroy() to fix problem with pthread library memory leak.

This commit is contained in:
kpl 2020-02-12 13:26:18 -05:00
parent b53383f6c3
commit 23f1fbf460

View File

@ -28,6 +28,8 @@ namespace cw
unsigned stateMicros; unsigned stateMicros;
unsigned pauseMicros; unsigned pauseMicros;
unsigned sleepMicros = 15000; unsigned sleepMicros = 15000;
pthread_attr_t attr;
} thread_t; } thread_t;
inline thread_t* _handleToPtr(handle_t h) { return handleToPtr<handle_t,thread_t>(h); } inline thread_t* _handleToPtr(handle_t h) { return handleToPtr<handle_t,thread_t>(h); }
@ -104,7 +106,6 @@ cw::rc_t cw::thread::create( handle_t& hRef, cbFunc_t func, void* funcArg, int s
{ {
rc_t rc; rc_t rc;
int sysRC; int sysRC;
pthread_attr_t attr;
if((rc = destroy(hRef)) != kOkRC ) if((rc = destroy(hRef)) != kOkRC )
return rc; return rc;
@ -117,19 +118,19 @@ cw::rc_t cw::thread::create( handle_t& hRef, cbFunc_t func, void* funcArg, int s
p->pauseMicros = pauseMicros; p->pauseMicros = pauseMicros;
p->stateId = kPausedThId; p->stateId = kPausedThId;
if((sysRC = pthread_attr_init(&attr)) != 0) if((sysRC = pthread_attr_init(&p->attr)) != 0)
{ {
p->stateId = kNotInitThId; p->stateId = kNotInitThId;
rc = cwLogSysError(kOpFailRC,sysRC,"Thread attribute init failed."); rc = cwLogSysError(kOpFailRC,sysRC,"Thread attribute init failed.");
} }
else else
if ((sysRC = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) != 0) if ((sysRC = pthread_attr_setdetachstate(&p->attr, PTHREAD_CREATE_DETACHED)) != 0)
{ {
p->stateId = kNotInitThId; p->stateId = kNotInitThId;
rc = cwLogSysError(kOpFailRC,sysRC,"Thread set detach attribute failed."); rc = cwLogSysError(kOpFailRC,sysRC,"Thread set detach attribute failed.");
} }
else else
if((sysRC = pthread_create(&p->pThreadH, &attr, _threadCallback, (void*)p )) != 0 ) if((sysRC = pthread_create(&p->pThreadH, &p->attr, _threadCallback, (void*)p )) != 0 )
{ {
p->stateId = kNotInitThId; p->stateId = kNotInitThId;
rc = cwLogSysError(kOpFailRC,sysRC,"Thread create failed."); rc = cwLogSysError(kOpFailRC,sysRC,"Thread create failed.");
@ -155,6 +156,10 @@ cw::rc_t cw::thread::destroy( handle_t& hRef )
if((rc = _waitForState(p,kExitedThId)) != kOkRC ) if((rc = _waitForState(p,kExitedThId)) != kOkRC )
return cwLogError(rc,"Thread timed out waiting for destroy."); return cwLogError(rc,"Thread timed out waiting for destroy.");
if( pthread_attr_destroy(&p->attr) != 0 )
rc = cwLogError(kOpFailRC,"Thread attribute destroy failed.");
mem::release(p); mem::release(p);
hRef.clear(); hRef.clear();