You said that the pointer is stored in another object, so that object's destructor would probably handle the deletion. Though that is not exactly the best design, IMO.
Thank You, I think it's the best solution. There are several points which I cannot understand. Following is the code
EnemyCharacter::EnemyCharacter(...)
{
TEnemyCharacter *tenemyCharacter = new TEnemyCharacter(this);
thread = new Thread(tenemyCharacter);
}
The Thread is a 3rd party wrapper class and not managed to find any deleting of this pointer
class Thread {
public:
Thread(IRunnable *ptr=0) {
_runnable = ptr;
_started = false;
_threadHandle = 0;
}
~Thread() {
if(_threadHandle != 0)
::CloseHandle(_threadHandle);
}
void start(IRunnable *ptr=0) {
if(_started)
throw ThreadException("Thread already started.");
if(!_started && _threadHandle != 0)
::CloseHandle(_threadHandle);
if(ptr != 0)
_runnable = ptr;
if(_runnable == 0)
throw ThreadException("An object implementing the IRunnable interface required.");
DWORD threadID=0;
_threadHandle = ::CreateThread(0, 0, ThreadProc, this, 0, &threadID);
if(_threadHandle == 0)
throw ThreadException(::GetLastError());
::Sleep(0);
}
void stop() {
checkAlive();
_runnable->stop();
}
void suspend() {
checkAlive();
checkThreadHandle();
if(::SuspendThread(_threadHandle) == -1)
throw ThreadException(::GetLastError());
}
void resume() {
checkAlive();
checkThreadHandle();
if(::ResumeThread(_threadHandle) == -1)
throw ThreadException(::GetLastError());
}
void join(unsigned long timeOut=INFINITE) {
checkThreadHandle();
if(isAlive()) {
DWORD waitResult = ::WaitForSingleObject(_threadHandle, timeOut);
if(waitResult == WAIT_FAILED)
throw ThreadException(::GetLastError());
}
}
bool isAlive() { return _started; }
protected:
bool _started;
HANDLE _threadHandle;
IRunnable *_runnable;
unsigned long run() {
_started = true;
unsigned long threadExitCode = _runnable->run();
_started = false;
return threadExitCode;
}
void checkThreadHandle() {
if(_threadHandle == 0)
throw ThreadException("Thread not yet created, call the start() method.");
}
void checkAlive() {
if(!isAlive())
throw ThreadException("No Thread alive.");
}
static unsigned long __stdcall ThreadProc(void* ptr) {
return ((Thread *)ptr)->run();
}
};
Should I put delete in the constructor of that class
~Thread() {
if(_threadHandle != 0)
::CloseHandle(_threadHandle);
delete _runnable; // addition
}