Click to See Complete Forum and Search --> : Memory deallocation in a thread


dullboy
May 24th, 2006, 06:55 PM
Here is my code,

void func(LPVOID item)
{
char* s = new char[100];
while(1)
{
}
delete[] s;
}

int main(int argc, char* argv[])
{
_beginthread(func,0,0);
Sleep(100);
return 0;
}


Actually, I failed to deallocate the memory of s. So what is the proper way to deallocate the memory allocated in a thread? Thanks for your inputs.

kuphryn
May 24th, 2006, 07:36 PM
func goes when main goes

s must go before main

Kuphryn

Arjay
May 24th, 2006, 08:16 PM
Here is my code,

void func(LPVOID item)
{
char* s = new char[100];
while(1)
{
}
delete[] s;
}

int main(int argc, char* argv[])
{
_beginthread(func,0,0);
Sleep(100);
return 0;
}


Actually, I failed to deallocate the memory of s. So what is the proper way to deallocate the memory allocated in a thread? Thanks for your inputs.Clearly the main thread will exit before the thread proc leaves the infinite while loop. In other words, as coded, the delete[] s; statement is unreachable code.

dullboy
May 24th, 2006, 11:21 PM
Thanks for the response. But how can I delete s in this situation?
Clearly the main thread will exit before the thread proc leaves the infinite while loop. In other words, as coded, the delete[] s; statement is unreachable code.

sirikrishnap
May 25th, 2006, 06:00 AM
Have a global variable or event and use it in the while condition.
Set it in main. Use WaitForSingleObject with thread HANDLE before exiting.

Arjay
May 25th, 2006, 12:24 PM
Thanks for the response. But how can I delete s in this situation?Asking again how to clean up s doesn't help me answer your question, because as the code is written, it will never reach the clean up code.

So you must be looking for something else. Like how to externally manage a thread?

If so, create an event and pass the handle to the event in the thread proc. On each iteration of the loop, check the event with WaitForSingleObject( hEvent, 0 ) and if it's set, break out of the loop. When the main thread wishes the secondary thread to exit, it sets the event, the secondary thread will break out of the loop and reach the string cleanup code.

void func(LPVOID item)
{
HANDLE hExitEvent = (HANDLE)item;
char* s = new char[100];
while(1)
{
if( WAIT_OBJECT_0 == WaitForSingleObject( hExitEvent, 0 ) )
break;

// Do other work
}
delete[] s;
}


That's one approach. Another way to approach this is to create a class that internally allocates it's resources and cleans them up in the destructor.

void func(LPVOID item)
{
CWorker worker( (HANDLE)item );
worker.DoWork( );
}


In in this code, we create a class called CWorker that allocates the memory for the string and provides a DoWork method that contains the while loop and the event check. In the constructor we pass in the event from the primary thread so we know when to exit the loop.

A final approach is to create a class that encapulates the thread creation and management code and uses a static thread proc method within the class. When the secondary thread is created, the *this* pointer is pass to the thread proc allowing the thread access to the classes data.

Here is a static thread proc for a CWorkMgr class. As I mentioned, the string data is allocated in the class, the thread is created, and the this pointer is passed in. From within the threadproc, the LPVOID is turned back in to a CWorkMgr pointer so the class's data can be accessed.


static UINT WINAPI CWorkMgr::ThreadProc( LPVOID lpContext )
{
CWorkMgr* pWorkMgr = static_cast< CWorkMgr* >( lpContext );

while( 1 )
{
// Check if thread should exit operation
if( WAIT_OBJECT_0 == WaitForSingleObject(
pWorkMgr->GetExitEvent(), 0 ) )
{
break;
}

//
// Do work
//
}
return 0;
}


For a real live implementation of this technique, search the forum for 'LVSelState' or see the article links in my signature (there is a LogSnd sample app within that uses this approach).

On thing to keep in mind is that if you move the data outside the secondary thread where it's accessed by the pri and sec threads, you will need to provide some thread synchronization to limit resource access. The articles/samples above also demonstrate this.