Jump to content

  • Log In with Google      Sign In   
  • Create Account

Of Threads and Things


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Psepha   Members   -  Reputation: 122

Like
Likes
Like

Posted 31 October 1999 - 10:02 PM

Hi all again.
A windows general coding question this time. Am just completing an network telephone program (game talkover type thing)and all appears to be going to plan (works) except where you close down the application. As you can hit the window close button at any time you can end up with resource leaks.....
eg
Function()
{
Buffer = new char[100];
Do something for a bit...
delete[] Buffer;
}

If you close the application whilst performing the Do something for a bit... code the delete[] function is not called. Hmm...I am assuming this is partly because the sound capture and network transmission side of the app runs on another thread....Hmmm...
Any ideas? Should I create some sort of global flag 'DontKillTheAppYetCosImBusy' type thing? Seems a bit messy.......

Psepha


Sponsor:

#2 CJ   Members   -  Reputation: 122

Like
Likes
Like

Posted 31 October 1999 - 08:17 AM

Why not just use the WM_CLOSE message, and delete your stuff there?

------------------
Dance with me......


#3 Psepha   Members   -  Reputation: 122

Like
Likes
Like

Posted 31 October 1999 - 08:44 AM

Umm - The WM_CLOSE message is received on the thread that controls the window - not the background task that controls the sound capture and network send processing. I guess the questio really is how do you close down a thread cleanly when it may need to interact with another thread that may be closing

#4 Alastair   Members   -  Reputation: 122

Like
Likes
Like

Posted 31 October 1999 - 08:49 AM

I assume that Function() is running in another thread. If you close down the app while the other thread is in its "Do something" bit then the "delete[] Buffer" will never be reached. This is because when the main process dies, all its threads die immediately, before they can cleanup.

One simple thing you can do set a global flag when you want all your extra threads to terminate. The threads should ocasionally look at this flag and exit their "Do something" bit when it is set.

To stop the main process from terminating before all the threads have cleaned up, you can use the WaitForSingleObject or WaitForMultipleObjects API functions. Just pass them the handle of your thread or threads and they will delay your main process until all the threads have exited.

Something like this:

/*** Main process ***/

bool g_finished = false;

// Called just before main process dies.
void OnQuit()
{
g_finished = true;
WaitForSingleObject(h_thread, INFINITE);
}


/*** Extra thread ***/

void Function()
{
Buffer = new char[100];
DoSomething();
delete[] Buffer;
}

void DoSomething()
{
while (!g_finished)
{
// do stuff
}
}


#5 felisandria   Members   -  Reputation: 739

Like
Likes
Like

Posted 31 October 1999 - 08:54 AM

If your window is aware of the thread you are worried about, you can ask the thread if it is finished during the WM_CLOSE message, and wait for it there, which is what I usually do.

-fel


#6 Splat   Members   -  Reputation: 122

Like
Likes
Like

Posted 31 October 1999 - 10:57 AM

Bascially, you combine the above two tactics. In the WM_CLOSE message, a global variable is set when threads are to clean up, then you WaitForMultipleObjects() for your threads to finish up, see the flag and quit, then you quit the main thread. Its not particularly hard or complex.

- Splat


#7 Psepha   Members   -  Reputation: 122

Like
Likes
Like

Posted 31 October 1999 - 10:02 PM

Thanks people - Alastair - Looks like the solution I'm gonna try - Seen it before but never used it

Thanks All

Psepha





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS