Hi,

I've been out of the country for a while so I've missed quite a bit of this
thread (please excuse that pun it was unintentional).
As such I may be well off base with this suggestion, but it seems plausible
to me.

If I remember rightly the rsThreadData was the set of parameters passed to
the threadproc as part of its LPVOID argument.
One mistake that I've made in the past is to allocate this structure on the
stack of another thread. This yields all sorts of bizarre
and random bugs as it can take a while for the other thread to obliterate
the data from its stack.

Consider

>>>>main thread....

void StartThread()
{

THREADPARAMS rsThreadData; // <---- rsThreadData is allocated on the main
thread's stack.

// Configure rsThreadData members

CreateThread( ....., &rsThreadData, ....);

// At this point rsThreadData is 'destroyed' but if it is a structure containing
no classes the data will still
// reside in memory and contain valid data until it is overwritten. It may
take a while for the main thread's
// stack to grow back to this point so the program will appear to work for
a while.

}

>>>> In the worker thread..

DWORD ThreadProc( LPVOID pParams)
{

THREADPARAMS* pThreadParams = (THREADPARAMS*)pParams; // <---- Is referring
to a structure stored somewhere
// in the main thread's stack!!!

}


To ensure this isn't the problem allocate the thread params on the heap.

void StartThread()
{

THREADPARAMS* pThreadData = new THREADPARAMS(); // <---- ThreadData is allocated
on the heap.

// Configure pThreadData members

CreateThread( ....., pThreadData, ....);

// Now at this point the pointer pThreadData is still valid.
}

DWORD ThreadProc( LPVOID pParams)
{

THREADPARAMS* pThreadParams = (THREADPARAMS*)pParams; // <---- Is referring
to a valid structure on the heap

......


delete pThreadParams; // Just before the thread exits.

}

Hope that helps, if not I'll just have  to go back and read the archives
a bit more.

Daniel


___________________________________________________________

Book yourself something to look forward to in 2005.
Cheap flights - http://www.tiscali.co.uk/travel/flights/
Bargain holidays - http://www.tiscali.co.uk/travel/holidays/




_______________________________________________
msvc mailing list
[email protected]
See http://beginthread.com/mailman/listinfo/msvc_beginthread.com for 
subscription changes, and list archive.

Reply via email to