Although it compiles fine, I get an error on rutime (it is an unexpected error) and I guess that I mess up with the pointer m_rt. Do you see any obvious mistakes?
You must always handle memory that you are sure will live throughout the course of the new thread. This means that you must not use the stack for thread parameters because the thread might try to access the data once it has gone out of scope.
You should do:
1 2 3 4 5 6
ThreadParams *ptp = new Threadparams();
ptp->window = (HWND)GetSafeHwnd();
ptp->mrt = m_rt;
//Now use ptp as argument for the thread.
//Since you allocated in the heap, the data will be available until it is deleted.
//Make the thread delete this memory.
threadparams in the main thread looks like it's a local variable to some function. The address of that local is passed to Thread1. But I bet the main thread returns from that function, which destroys the variable that was passed to Thread1, but Thread1 continues to refer to it.
You should consider allocating threadparams from the heap.
The main thread allocated RAM using the new operator. To avoid a memory leak, this must be deleted once it is not used. Typically, the new thread does this when the data is no longer needed.
1 2 3 4 5 6 7 8 9 10 11
//This is the new thread's code:
UINT Thread1(LPVOID lp)
{
//Reinterpret the data.
ThreadParams *data = reinterpret_cast<ThreadParams*>(lp);
//Work.
...
//Ok, data no longer needed. Delete the data through the proper pointer.
delete data;
//Don't delete using the pointer variable 'lp' because you want proper destruction.
}