Reputation: 99
I have code like this
X *m = new X(); // X - class derived from CWinThread;
m->CreateThread(CRREATE_THREAD);
m->b_AutoDelete = false;
x->ResumeThread;
in function run I have some code, that runs in the loop.
When I try to do delete m
, often I get an exception.
Do I need to do something before delete
, may be m->Suspend
or something else?
Code runs on Windows 7
Upvotes: 0
Views: 974
Reputation: 409412
It's hard to say, but it might crash because you kind of pull the rug out from under the feet of the thread when deleting the object.
The nice way to end a thread is to first tell it to stop. This is often done with a boolean flag that the thread function checks. It will let the thread function handle cleanup in a nice way. Once that is done, which can be checked with another boolean flag, you are then free to delete the object.
Upvotes: 2