Memmory Issues and Threads

This is a discussion on Memmory Issues and Threads within the Windows Programming forums, part of the Platform Specific Boards category; I am writing a replacement GINA for windows XP. My logon dialog box starts a thread (using CreateThread()) when it ...

  1. #1
    Registered User
    Join Date
    Aug 2004

    Memmory Issues and Threads

    I am writing a replacement GINA for windows XP. My logon dialog box starts a thread (using CreateThread()) when it loads. The thread is passed the handle to the dialog box and then monitors the the registry for specific changes in order to adjust my dialog box's controls accordingly. Whenever the dialog is refreshed or closed I send a TerminateThread() command to end the thread so that when the logon dialog box reloads itself I dont't get multiple threads running. However this method seems to be causing some memory issues. According to MSDN, TerminateThread() is a risky call for this reason.

    "If the target thread is allocating memory from the heap, the heap lock will not be released."
    I am new to C and know almost nothing about memory allocation. All I am doing in the thread is getting some handles to some graphic controls and using those handles to manipulate the graphics on the dialog box. I have noticed that everytime i refresh the dialog, windows alocates more memory. This can continue until the machine has no available memory left. It seems to me that the above quote would explain this memory leak.

    Is there a command I can call everytime I terminate the thread that will also look for unused memory and de-allocate it? Thanks for your help.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Small world - I'm working on a Gina replacement myself

    You are responsible for your threads clean termination. A clean termination simply implies that the thread deallocates any allocated resources and returns from the thread function. So all that's really needed is a mechanism to "tell" your thread is time to return.
    You can use bool variable or a Win32 synchronization object like an event - whatever works.


  3. #3
    Registered User
    Join Date
    Aug 2004
    That was how I originally intented to set it up. However the thread takes about ten seconds to completes one cycle of its loop. Here is what happens.

    Works fine here
    Dialog is started and sets the thread's bool to true.
    user logs on, dialog closes and bool is set to false
    thread closes appropriately

    Bad scenario
    Dialog is started and sets the thread's bool to true.
    user hits cancel and dialog closes setting the bool to false
    the dialog immediately is called again setting the bool to true
    the thread never sees the bool as false and continues to run
    A new thread is started since it is called from the dialog box.

    Basically the screensaver, a dialog timeout, or the user hitting cancel cause another thread to open before the other thread knows to terminate itself causing multiple threads to be open at one time. This seems to be a waste. However once the user logs onto the machine the bool will be set to false and all the the threads will have time to recognize the bool as false and terminate.

    You know as I am writing this I am thinking of a more efficient way to call the thread globally, outside of the dialog callback.

    I was just hoping there was a simple command that would perform some garbage collection and free up all the un-linked resources. That way I wouldn't have to re-structure my code. Thanks for your response.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 33
    Last Post: 05-14-2009, 11:15 AM
  2. gcnew : alignment issues?
    By mynickmynick in forum C++ Programming
    Replies: 1
    Last Post: 08-27-2008, 03:42 AM
  3. Parallelism
    By cerin in forum Tech Board
    Replies: 17
    Last Post: 06-02-2007, 12:14 AM
  4. Threads, Linked Lists, Semaphores, Critical Section ...
    By _jr in forum Windows Programming
    Replies: 4
    Last Post: 06-21-2006, 09:14 AM
  5. How to make the program not to wait for the function?
    By maxorator in forum C++ Programming
    Replies: 10
    Last Post: 10-02-2005, 04:02 PM
