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
정성태

... 121  122  123  124  125  126  127  128  [129]  130  131  132  133  134  135  ...
NoWriterDateCnt.TitleFile(s)
1828정성태12/15/201427631VC++: 85. Windows Vista부터 바뀐 Credential Provider 예제 분석 (1) [4]파일 다운로드1
1827정성태12/12/201423991VC++: 84. CredUIPromptForWindowsCredentials Win32 API 사용법 정리
1826정성태12/11/201428488.NET Framework: 489. Socket.Listen에 전달된 backlog 인자의 의미 [6]
1825정성태12/11/201475656.NET Framework: 488. TCP 소켓 연결의 해제를 알 수 있는 방법 [10]파일 다운로드1
1824정성태12/10/201425715.NET Framework: 487. Socket.Receive 메서드의 SocketFlags.Peek 동작을 이용해 소켓 연결 유무를 확인? [8]파일 다운로드1
1823정성태12/10/201422635.NET Framework: 486. Java의 ScheduledExecutorService에 대응하는 C#의 System.Threading.Timer [2]
1822정성태12/3/201425304개발 환경 구성: 252. Xamarin 라이선스 관리 [8]
1821정성태12/1/201431674VS.NET IDE: 96. Visual Studio에서 /* ~ */ 주석을 위한 단축키(매크로)
1820정성태11/23/201420456.NET Framework: 485. Mono Profiler에서 IL 코드 변경이 가능할까? - 두 번째 이야기파일 다운로드1
1819정성태11/22/201420436오류 유형: 264. MSBuild 에러 - warning MSB3052: The parameter to the compiler is invalid,
1818정성태11/22/201422862개발 환경 구성: 251. NUMA 노드 수에 따른 IIS Worker Process(w3wp.exe) 설정 [1]
1817정성태11/22/201426007VS.NET IDE: 95. Macros for Visual Studio 2013 [1]
1816정성태11/20/201420690.NET Framework: 484. Mono Profiler에서 IL 코드 변경이 가능할까?
1815정성태11/18/201418658.NET Framework: 483. 코드로 살펴 보는 ETW의 활성화 시점 [2]
1814정성태11/18/201420239오류 유형: 263. Unable to find the requested .Net Framework Data Provider. It may not be installed.
1813정성태11/18/201422105오류 유형: 262. Build Events에 robocopy 작업이 있는 경우 "VCEnd exited with code 3" 오류 발생
1812정성태11/17/201419537.NET Framework: 482. ETW 자동 생성 코드의 VerificationException 예외 발생
1811정성태11/16/201420863.NET Framework: 481. Mono 내부의 문자열 처리 방식은 UTF-8
1810정성태11/15/201457511.NET Framework: 480. C# - 배치 파일 실행하고 출력 결과를 얻는 방법 [4]
1809정성태11/11/201422743.NET Framework: 479. Mono Profiler를 Unity의 Plugin으로 사용하는 방법
1808정성태11/11/201423062.NET Framework: 478. C# - 폴더 경로 문자열에서 "..", "." 표기를 고려한 최종 문자열을 얻는 방법 [2]
1807정성태11/10/201429237개발 환경 구성: 250. 서버용 Socket에서 사용하는 포트가 충돌한다면?파일 다운로드1
1806정성태11/10/201425021.NET Framework: 477. SeCreateGlobalPrivilege 특권과 WCF NamedPipe
1805정성태11/5/201421845.NET Framework: 476. Visual Studio에서 Mono용 Profiler 개발 [3]파일 다운로드1
1804정성태11/5/201428103.NET Framework: 475. ETW(Event Tracing for Windows)를 C#에서 사용하는 방법 [9]파일 다운로드1
1803정성태11/4/201420210오류 유형: 261. Windows Server Backup 오류 - Error in backup of E:\$Extend\$RmMetadata\$TxfLog
... 121  122  123  124  125  126  127  128  [129]  130  131  132  133  134  135  ...