CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Apr 2012
    Posts
    29

    Question C2712: Cannot use __try in functions that require object unwinding

    Hello,

    I am migrating a software writen with VC++ 6.0 to VS 2010/VC++2010. and I have a project which does not compile in Debug mode. I have:

    Code:
    LBSC_AppUserInfoList *			
    LBSC_Session::GetUsersInfo( const LBSC_Ticket *pTicket )
    {
    	TSROut_GetUsersInfo	sOut;
    	C_Buffer			*pcbRet = NULL;
    	TSRIn_TicketPar		InPar;
    //...
    	ClntFree( (void *)sOut.szBuffer );  // dealoca o que veio do RPC
    
    	return( pList );
    } // C2712 - Cannot use __try in functions that require object unwinding
    and I haven´t any mention to __try

    do you undestand?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: C2712: Cannot use __try in functions that require object unwinding

    What is TSROut_GetUsersInfo ?
    What is C_Buffer?
    What is TSRIn_TicketPar ?
    What is ClntFree?
    What is hidden under //...?

    What line of this code snippet produced the error"C2712 - Cannot use __try in functions that require object unwinding"?
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C2712: Cannot use __try in functions that require object unwinding

    Quote Originally Posted by michaelhsilva View Post
    return( pList );
    Where did "pList" come from? It just pops up out of nowhere in your code.

    The bottom line is that if you have a problem compiling code, it doesn't help if you're hiding code with comments or introduce variables and types no one knows what they are. It could be what's in those comments or maybe something is strange with the types you're using.
    Code:
    LBSC_AppUserInfoList *			
    LBSC_Session::GetUsersInfo( const LBSC_Ticket *pTicket )
    {
    	return 0;
    }
    Still we don't know what LBSC_AppUserInfoList or what those types are, but if you took everything out of that function and just returned NULL, does the error go away? If it does go away, then it's all of those types we don't know about and/or the hidden code that is causing the problem.

    Regards,

    Paul McKenzie

  4. #4
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: C2712: Cannot use __try in functions that require object unwinding

    Also, have you look at Compiler Error C2712?

  5. #5
    Join Date
    Apr 2012
    Posts
    29

    Question Re: C2712: Cannot use __try in functions that require object unwinding

    I´ll put here the entire code. The line that pops the error up is the last '}'

    Code:
    LBSC_AppUserInfoList *			
    LBSC_Session::GetUsersInfo( const LBSC_Ticket *pTicket )
    {
    	TSROut_GetUsersInfo	sOut;
    	C_Buffer			*pcbRet = NULL;
    	TSRIn_TicketPar		InPar;
    	
    
    	unsigned long ulExceptionCode = 0;
    	memset( (void*) &InPar, 0, sizeof(InPar));
    	if( !LBSC_ClntNet::GetRPCHandle( (char *)this->ServerAddr ) ){
    		return( NULL );
    	}
    
    	InPar.sTicket = BuildTickBuff(pTicket);
    	InPar.sCtrlInfo = ObtemControlInfo();
    	InPar.lSessionObj	= this->obj;
    
    	boolean bNetError = TRUE;
    	boolean bRetry = FALSE;
    	do {
    		TSRIn_TicketPar		DupInPar;
    		RpcTryExcept{
    			Duplicate( InPar, &DupInPar );
    			Criptografa( DupInPar );
    
    			sOut = RPC_Session_GetUsersInfo( DupInPar );
    			bNetError = FALSE;
    			bRetry = FALSE;
    
    			Free( DupInPar );
    		}
    		RpcExcept(1){
    			Free( DupInPar );
    
    			bRetry = LBSC_ClntNet::EnviaNovaRPC();
    			ulExceptionCode = RpcExceptionCode();
    			
    		}
    		RpcEndExcept
    	} while( bRetry );
    	LBSC_ClntNet::FreeRPCHandle();
    	if( bNetError ) {
    		LBSC_ClntNet::TrataErroRPC( (char *)this->ServerAddr  , ulExceptionCode);
    		return(	NULL );		
    	}
    										
    	if ( !sOut.lTamBuff ) {
    		return ( NULL );
    	}
    
    	LBSC_AppUserInfoList	*pList = NULL;
    	pcbRet = new C_Buffer( sOut.lTamBuff - sizeof(int) ); // pula o int do cbuffer; Vladimir Catao 29/01/96
    	if( !pcbRet ){	// sem memoria, considere um erro de rede; trate-o
    		return ( NULL );
    	}
    
    	pcbRet->PutData( (void*) (sOut.szBuffer + sizeof(int)), sOut.lTamBuff - sizeof(int)); // pula o int do cbuffer; Vladimir Catao 29/01/96
    	pcbRet->Rewind();
    	pList = MakeAppUserInfoList( *pcbRet );
    	delete pcbRet;
    	ClntFree( (void *)sOut.szBuffer );  // dealoca o que veio do RPC
    
    	return( pList );
    }

  6. #6
    Join Date
    Apr 2012
    Posts
    29

    Question Re: C2712: Cannot use __try in functions that require object unwinding

    Quote Originally Posted by Ejaz View Post
    Also, have you look at Compiler Error C2712?
    I´ve seen, but does not help the code doesn´t mention __try...

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C2712: Cannot use __try in functions that require object unwinding

    Quote Originally Posted by michaelhsilva View Post
    I´ll put here the entire code. The line that pops the error up is the last '}'
    What is this?
    Code:
    RpcTryExcept
    Also, did you do as I stated? Take all of that code out and just return NULL. Does the error go away? If it does, then reintroduce the code one segment at a time until the error shows up again. Then you know what causes the error and you can then further investigate.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Apr 2012
    Posts
    29

    Question Re: C2712: Cannot use __try in functions that require object unwinding

    I´ve commented the body of the function and put return 0, but the error run to another line a ´}´ too...

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C2712: Cannot use __try in functions that require object unwinding

    Quote Originally Posted by michaelhsilva View Post
    I´ve commented the body of the function and put return 0, but the error run to another line a ´}´ too...
    I don't understand. Does the error for that function in question go away? It isn't important if another function has an error, does the function you commented the code out of have an error now?

    You also didn't answer what that "RpcTryExcept" is or what it does.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Apr 2012
    Posts
    29

    Smile Re: C2712: Cannot use __try in functions that require object unwinding

    A ha!!!

    i found

    Code:
    #define RpcTryExcept \
        __try \
            {
    
    // trystmts
    
    #define RpcExcept(expr) \
            } \
        __except (expr) \
            {
    
    // exceptstmts

  11. #11
    Join Date
    Apr 2012
    Posts
    29

    Smile Re: C2712: Cannot use __try in functions that require object unwinding

    A ha!!!

    i found

    Code:
    #define RpcTryExcept \
        __try \
            {
    
    // trystmts
    
    #define RpcExcept(expr) \
            } \
        __except (expr) \
            {
    
    // exceptstmts
    Regards,
    Michael

  12. #12
    Join Date
    Apr 2012
    Posts
    29

    Thumbs up Re: C2712: Cannot use __try in functions that require object unwinding

    I´ll do like you posted Paul.

    Regards,
    Michael

  13. #13
    Join Date
    Apr 2012
    Posts
    29

    Question Re: C2712: Cannot use __try in functions that require object unwinding

    I need Help!

    http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx says that: C2712 can occur if you compile with /clrure and declare a static array of pointers-to-functions in a __try block.

    I am not compiling with /clrure...

    Paul, I did not tested line by line yet but making a test project with the code
    Code:
    RpcTryExcept{
    
    	}
    	RpcExcept(1){
    		
    	}
    	RpcEndExcept
    it compiles well but in the project of VC++ 6.0, that I am migrating to VS 2010, does not compile, in other words the __try block without code inside.

    could you help me?

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C2712: Cannot use __try in functions that require object unwinding

    Quote Originally Posted by michaelhsilva View Post
    I need Help!

    http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx says that: C2712 can occur if you compile with /clrure and declare a static array of pointers-to-functions in a __try block.

    I am not compiling with /clrure...
    Did you read the beginning of that page?
    With /EHsc, a function with structured exception handling cannot have objects that require unwinding (destruction).

    Possible solutions:

    Move code that requires SEH to another function.

    Rewrite functions that use SEH to avoid the use of local variables and parameters that have destructors. Do not use SEH in constructors or destructors.

    Compile without /EHsc.
    The reason and possible solutions are in the quote above. What you mentioned is just one possible reason, not the only reason for the issue.

    Why not do something I've mentioned three times now? Remove code from the __try/__except until you get a good compile. When you do get a good compile, you know exactly which functions need to be moved, changed, whatever, to get all of your code to compile.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; May 18th, 2012 at 01:16 PM.

  15. #15
    Join Date
    Apr 2012
    Posts
    29

    Thumbs up Re: C2712: Cannot use __try in functions that require object unwinding

    Quote Originally Posted by Paul McKenzie View Post
    Did you read the beginning of that page?
    The reason and possible solutions are in the quote above. What you mentioned is just one possible reason, not the only reason for the issue.

    Why not do something I've mentioned three times now? Remove code from the __try/__except until you get a good compile. When you do get a good compile, you know exactly which functions need to be moved, changed, whatever, to get all of your code to compile.

    Regards,

    Paul McKenzie
    I understand now that it´s not the code inside __try / __except because only __try /] __except itself make the error appear without code inside. I´ve moved the __try / __except block to another function passing the right parameters and it compiles.

    Thank you very much.

    Regards,
    Michael

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured