user1913557
user1913557

Reputation: 99

How to delete an object derived from CWinThread

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

Answers (1)

Some programmer dude
Some programmer dude

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

Related Questions