Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일

Visual C++ 컴파일 오류 - Cannot use __try in functions that require object unwinding

"Cannot use __try in functions that require object unwinding" 오류에 대해 MSDN에 다음과 같은 설명이 있습니다.

Compiler Error C2712
; https://docs.microsoft.com/en-us/cpp/error-messages/compiler-errors-2/compiler-error-c2712

위의 내용을 간단한 예제들과 함께 살펴볼까요? ^^

우선, 소멸자가 없는 클래스를 메서드에 포함한 후 __try/__finally를 사용하면 아무런 이상이 없습니다.

class CTestClassWithoutDtor
{
public:
    CTestClassWithoutDtor() { }
};

void CTest::UseTestClassWithoutDtor()
{
    CTestClassWithoutDtor dtor;
    __try
    {

    }
    __finally
    {

    }
}

하지만, __try/__finally를 포함한 메서드에 소멸자가 있는 클래스를 사용하는 경우에는 C2712 오류가 발생합니다.

class CTestClassWithDtor
{
public:
    CTestClassWithDtor() { }
    ~CTestClassWithDtor() { }
};

void CTest::UseTestClassWithDtor()
{
    CTestClassWithDtor dtor; // error C2712 : Cannot use __try in functions that require object unwinding
    __try
    {

    }
    __finally
    {

    }
}

이는 포인터로 사용해도 마찬가지입니다.

void CTest::CreateTestClassWithDtor()
{
    CTestClassWithDtor *pDtor = new CTestClassWithDtor(); // error C2712 : Cannot use __try in functions that require object unwinding

    __try
    {

    }
    __finally
    {

    }
}

가장 이상적인 해결책은 물론 소멸자를 가진 클래스를 사용하지 않는 것입니다. 하지만, 이게 말처럼 쉬운 것이 아닙니다. 다 써야할 상황이 되니 쓰고 있는 거니까요.

대신, 생성을 우회하면 해결할 수 있습니다. 즉, new 했던 것을 다음과 같이 별도의 함수로 빼서 처리하면 됩니다.

CTestClassWithoutDtor *CTest::ClassWithoutDtorFactory() 
{ 
    return new CTestClassWithoutDtor(); 
}

void CTest::CreateTestClassWithoutDtor()
{
    // CTestClassWithoutDtor *pDtor = new CTestClassWithoutDtor();

    CTestClassWithoutDtor *pDtor = ClassWithoutDtorFactory();

    __try
    {

    }
    __finally
    {

    }
}




그 외에 STL 라이브러리에서 제공되는 클래스를 무심코 사용하다 보면 이렇게 C2712 오류가 발생합니다.

// error C2712 : Cannot use __try in functions that require object unwinding
void CTest::OutputString(wstring txt)
{
    __try
    {

    }
    __finally
    {

    }
}

왜냐하면, wstring같은 클래스들이 기본적으로 소멸자를 정의하고 있기 때문입니다. 이렇게 함수의 인자로 전달하는 경우는 포인터로 전달하면 오류를 우회할 수 있습니다.

void CTest::OutputString2(wstring *pTxt)
{
    __try
    {

    }
    __finally
    {

    }
}

아니면, 인스턴스가 스택상에 생성되지 않도록 참조형으로 전달하는 것도 가능합니다.

void CTest::OutputString(wstring &txt)
{
    __try
    {

    }
    __finally
    {

    }
}

대충... 감이 오시죠? ^^

(첨부한 파일은 위의 예제 코드를 포함합니다.)




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 7/17/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2014-08-12 01시36분
아래의 글에 Visual C++ 예외와 관련해 "Enable C++ Exceptions" 옵션을 잘 설명해 주고 있으니 참고하세요. ^^

try~catch 와 __try~__except 의 차이점
; http://kuaaan.tistory.com/435
정성태

1  [2]  3  4  5  6  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13594정성태4/12/20241115닷넷: 2236. C# - Audio 장치 열람 (Windows Multimedia, NAudio)파일 다운로드1
13593정성태4/8/20241138닷넷: 2235. MSBuild - AccelerateBuildsInVisualStudio 옵션
13592정성태4/2/20241603C/C++: 165. CLion으로 만든 Rust Win32 DLL을 C#과 연동 [1]
13591정성태4/2/20241516닷넷: 2234. C# - WPF 응용 프로그램에 Blazor App 통합파일 다운로드1
13590정성태3/31/20241403Linux: 70. Python - uwsgi 응용 프로그램이 k8s 환경에서 OOM 발생하는 문제
13589정성태3/29/20241501닷넷: 2233. C# - 프로세스 CPU 사용량을 나타내는 성능 카운터와 Win32 API파일 다운로드1
13588정성태3/28/20241867닷넷: 2232. C# - Unity + 닷넷 App(WinForms/WPF) 간의 Named Pipe 통신 [2]파일 다운로드1
13587정성태3/27/20241479오류 유형: 900. Windows Update 오류 - 8024402C, 80070643
13586정성태3/27/20241851Windows: 263. Windows - 복구 파티션(Recovery Partition) 용량을 늘리는 방법
13585정성태3/26/20241584Windows: 262. PerformanceCounter의 InstanceName에 pid를 추가한 "Process V2"
13584정성태3/26/20241529개발 환경 구성: 708. Unity3D - C# Windows Forms / WPF Application에 통합하는 방법파일 다운로드1
13583정성태3/25/20241578Windows: 261. CPU Utilization이 100% 넘는 경우를 성능 카운터로 확인하는 방법
13582정성태3/19/20241687Windows: 260. CPU 사용률을 나타내는 2가지 수치 - 사용량(Usage)과 활용률(Utilization)파일 다운로드1
13581정성태3/18/20241803개발 환경 구성: 707. 빌드한 Unity3D 프로그램을 C++ Windows Application에 통합하는 방법
13580정성태3/15/20241333닷넷: 2231. C# - ReceiveTimeout, SendTimeout이 적용되지 않는 Socket await 비동기 호출파일 다운로드1
13579정성태3/13/20241532오류 유형: 899. HTTP Error 500.32 - ANCM Failed to Load dll
13578정성태3/11/20241713닷넷: 2230. C# - 덮어쓰기 가능한 환형 큐 (Circular queue)파일 다운로드1
13577정성태3/9/20242024닷넷: 2229. C# - 닷넷을 위한 난독화 도구 소개 (예: ConfuserEx)
13576정성태3/8/20241641닷넷: 2228. .NET Profiler - IMetaDataEmit2::DefineMethodSpec 사용법
13575정성태3/7/20241775닷넷: 2227. 최신 C# 문법을 .NET Framework 프로젝트에 쓸 수 있을까요?
13574정성태3/6/20241868닷넷: 2226. C# - "Docker Desktop for Windows" Container 환경에서의 IPv6 DualMode 소켓
13573정성태3/5/20241797닷넷: 2225. Windbg - dumasync로 분석하는 async/await 호출
13572정성태3/4/20241832닷넷: 2224. C# - WPF의 Dispatcher Queue로 알아보는 await 호출의 hang 현상파일 다운로드1
13571정성태3/1/20241935닷넷: 2223. C# - await 호출과 WPF의 Dispatcher Queue 동작 확인파일 다운로드1
13570정성태2/29/20241884닷넷: 2222. C# - WPF의 Dispatcher Queue 동작 확인파일 다운로드1
13569정성태2/28/20241787닷넷: 2221. C# - LoadContext, LoadFromContext 그리고 GAC파일 다운로드1
1  [2]  3  4  5  6  7  8  9  10  11  12  13  14  15  ...