Using a Termination Handler
The following example shows how a termination handler is used to ensure that resources are released when execution of a guarded body of code terminates. In this case, a thread uses the EnterCriticalSection function to wait for ownership of a critical section object. When the thread is finished executing the code that is protected by the critical section, it must call the LeaveCriticalSection function to make the critical section object available to other threads. Using a termination handler guarantees that this will happen. For more information, see critical section objects.
LPTSTR lpBuffer = NULL;
CRITICAL_SECTION CriticalSection;
// EnterCriticalSection synchronizes code with other threads.
EnterCriticalSection(&CriticalSection);
__try
{
// Perform a task that may cause an exception.
lpBuffer = (LPTSTR) LocalAlloc(LPTR, 10);
StringCchCopy(lpBuffer, 10, TEXT("Hello"));
_tprintf(TEXT("%s\n"),lpBuffer);
LocalFree(lpBuffer);
}
__finally
{
// LeaveCriticalSection is called even if an exception occurred.
LeaveCriticalSection(&CriticalSection);
}
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기