- Code: Select all
void ThreadImpl::startImpl(Runnable& target)
{
....
_pRunnableTarget = ⌖
....
}
Here, pRunnableTarget should contain a deep copy of the target argument, not just a pointer to it.
Since pRunnableTarget is used at the moment the thread is spawned (asynchronously to this call).
And if the Runnable object passed to startImpl has been destructed meanwhile, the process is likely to crash.
I have created a simple example that crashes on my machine every time (Windows 7):
- Code: Select all
#include "Poco/Thread.h"
#include "Poco/Event.h"
class Runner
{
public:
void Start();
void Stop();
void ThreadProc();
private:
void DoSomeWork();
private:
Poco::Thread m_Thread;
Poco::Event m_StopHandle;
};
int main()
{
Runner r;
r.start();
Poco::Thread::sleep(5000);
return 0;
}
void Runner::Start()
{
RunnableAdapter<Runner> ra(*this, &Runner::ThreadProc);
m_Thread.start(ra);
}
void Runner::Stop()
{
m_StopHandle.set();
}
void Runner::DoSomeWork()
{
/*...*/
}
void Runner::ThreadProc()
{
while (true)
{
bool result = m_StopHandle.tryWait(500);
if (result)
{
//stop request -> terminate
return;
}
DoSomeWork();
}
}





