Thread & Class
-
Hi I have a problem with the thread and class. I created a thread from a function and in the thread function, i called and init a class but not using the new and delete method. There is a problem when the thread ended as the class destructor was not activated when i called ExitThread...an example below main function() { CreateThread(.....,ThreadProc,...); } ThreadProc() { CDB_Database Data(); //Class Init DWORD dwRecords; HANDLE hFile; etc OpenFile(); ... .... ... ExitThread(0); } my problem is everytime i called creatThread, the class constructor will be activated but when i called ExitThread, the class destructor was not activated. So is the resources used by the thread free when i called exitThread as i seems to have sharing violation inside the function Thank you for ur advise
-
Hi I have a problem with the thread and class. I created a thread from a function and in the thread function, i called and init a class but not using the new and delete method. There is a problem when the thread ended as the class destructor was not activated when i called ExitThread...an example below main function() { CreateThread(.....,ThreadProc,...); } ThreadProc() { CDB_Database Data(); //Class Init DWORD dwRecords; HANDLE hFile; etc OpenFile(); ... .... ... ExitThread(0); } my problem is everytime i called creatThread, the class constructor will be activated but when i called ExitThread, the class destructor was not activated. So is the resources used by the thread free when i called exitThread as i seems to have sharing violation inside the function Thank you for ur advise
The
ExitThread()
function never returns, so theThreadProc()
doesn't reach its end and destructors don't get called. One way to solve this is to add another pair of braces:ThreadProc()
{
{
CDB_Database Data(); //Class Init
DWORD dwRecords;
HANDLE hFile; etcOpenFile(); //...
}
ExitThread(0);
}Now
Data
is destroyed when code exits the inner braces, just beforeExitThread()
-
Hi I have a problem with the thread and class. I created a thread from a function and in the thread function, i called and init a class but not using the new and delete method. There is a problem when the thread ended as the class destructor was not activated when i called ExitThread...an example below main function() { CreateThread(.....,ThreadProc,...); } ThreadProc() { CDB_Database Data(); //Class Init DWORD dwRecords; HANDLE hFile; etc OpenFile(); ... .... ... ExitThread(0); } my problem is everytime i called creatThread, the class constructor will be activated but when i called ExitThread, the class destructor was not activated. So is the resources used by the thread free when i called exitThread as i seems to have sharing violation inside the function Thank you for ur advise
If the ThreadProc is declared as follows: DWORD WINAPI ThreadProc( LPVOID lpParameter // thread data ); Why don't you just return 0; from your thread function instead of calling ExitThread at the every end of it anyway? Then your variables can go out of scope and destroy themselves automatically. Anything monitoring the 'exit code' of the thread will still see the 0. FYI (From MSDN): A thread that uses functions from the C run-time libraries should use the _beginthread and _endthread C run-time functions for thread management rather than CreateThread and ExitThread. Failure to do so results in small memory leaks when ExitThread is called.