When the process ends, the OS will return all the allocated memory and close all open descriptors. You do not need to worry about MEMORY *), which occurs in the special case of closing the application. OS will also close all your open handles **), at least theoretically. All that has been taken into account may be safe for you to simply terminate your thread (using TerminateThread (MyThread.Handle) ) from your form destructor before destroying other shared resources. Ask yourself the following questions:
- What does a stream do? Is it safe to terminate it at any time? Example. If a stream writes to disk, it is unsafe to simply kill it, because you can write files to disk in an inconsistent state.
- Are you using any resources that are not automatically released by Windows? Can't come up with a good example here ...
If you are safe with both, you can use TerminateThread and not wait for the thread to terminate naturally. A safer approach can be combined, perhaps you should give the thread the chance to naturally terminate, and if it does not exit in 5 seconds, force it to stop.
*) I'm talking about memory, you can only prove a leak at the end of the process, for example, a leak that you kill, preventing them from closing correctly or global singleton classes that you are not freeing. All other unaccounted memory must be tracked and corrected, because this is an error.
**) Unfortunately, Windows is not an error. Example. Anyone who has been working with serial devices on the Windows platform knows how easy it is to get a serial device in a “locked” state, requiring a reboot so that it works again. Technically, this is also Handle, which completes the processing of the application that blocked it, should unlock it.
source share