Share via


PREfast Warning 258 (Windows CE 5.0)

Send Feedback

258 - Using TerminateThread does not allow proper thread clean up.
Additional Information: Consult MSDN for more information.

This warning indicates that a call to TerminateThread has been detected.

TerminateThread is a dangerous function that should only be used in the most extreme cases.

Only call TerminateThread if you know exactly what the target thread is doing, and only if you control all code the target thread could possibly be running at the time of the termination.

For example, TerminateThread can result in the following problems:

  • If the target thread owns a critical section, the critical section will not be released.
  • If the target thread is executing certain kernel32 calls when it is terminated, the kernel32 state for the thread's process could be inconsistent.
  • If the target thread is manipulating the global state of a shared DLL, the state of the DLL could be destroyed, affecting other users of the DLL.

A thread cannot protect itself against TerminateThread, other than by controlling access to its handles. The thread handle returned by the CreateThread and CreateProcess functions has THREAD_TERMINATE access, so any caller holding one of these handles can terminate your thread.

Note   SuspendThread has similar problems, but PREfast does not currently detect use of this API.

Example

Defective Source

TerminateThread(Thread,
                0);

Corrected Source

extern HANDLE ThreadTerminationEvent[];
SetEvent(ThreadTerminationEvent[(ULONG)Thread]);
/*
* Target thread waits on/polls the termination event
* and gracefully releases locks and resources before
* calling ExitThread().
*/

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.