Throwing exceptions in ATL dll with optimize for SPEED causes Access Violation 
Author Message
 Throwing exceptions in ATL dll with optimize for SPEED causes Access Violation

Hello,

We have generated an ATL COM DLL (using the wizard) in which we throw and
catch exceptions (C++) and return a COM Error as follows:

try {
    int i = 20;
    throw i;

Quote:
}

catch (int & e) {
    return Error("I caught an exception");

Quote:
}

return S_OK;

This code compiles but causes access violation if we use the compiler
optimization to maximize speed. No problem if optimized for size.

The access violation occurs as soon as we return the "Error" and not at
throw time.

I was wondering if anyone could shed light on this.

Thanks

Prasenjit



Sun, 16 Mar 2003 03:00:00 GMT  
 Throwing exceptions in ATL dll with optimize for SPEED causes Access Violation

The only thing I can think of is that Error() uses AtlReportError() which
uses A2COLE() which uses (ultimately) A2W which uses _alloca() and that is
not supposed to be used in a try/catch block..
Check out KB article
PRB: Calling String Conversion Macros in Catch Block Causes AV
ID: Q198009 .. That explains the problem..

That might be the problem. To see if it helps, convert that string manually
into a wide char using L"" and see if that works..

--
Girish Bharadwaj

Quote:
> Hello,

> We have generated an ATL COM DLL (using the wizard) in which we throw and
> catch exceptions (C++) and return a COM Error as follows:

> try {
>     int i = 20;
>     throw i;
> }
> catch (int & e) {
>     return Error("I caught an exception");
> }

> return S_OK;

> This code compiles but causes access violation if we use the compiler
> optimization to maximize speed. No problem if optimized for size.

> The access violation occurs as soon as we return the "Error" and not at
> throw time.

> I was wondering if anyone could shed light on this.

> Thanks

> Prasenjit



Tue, 18 Mar 2003 03:00:00 GMT  
 Throwing exceptions in ATL dll with optimize for SPEED causes Access Violation
Hello,

Thanks for the reply.

My understanding of that limitation is that because _alloca allocates on the
stack and should not be called directly from a try-catch block. It is
however OK to call it from a function which is called from the try-catch
block. In this case, the call stack looks as follows:

AtlSetErrorInfo
AtlReportError        inline  calls _alloca
Error                       non-inline
Function calling error in catch block

I realize that AtlReportError is inlined away on optimize for speed, but
there is still the Error() function which would provide the necessary stack

Thanks again.

Prasenjit


Quote:
> The only thing I can think of is that Error() uses AtlReportError() which
> uses A2COLE() which uses (ultimately) A2W which uses _alloca() and that is
> not supposed to be used in a try/catch block..
> Check out KB article
> PRB: Calling String Conversion Macros in Catch Block Causes AV
> ID: Q198009 .. That explains the problem..

> That might be the problem. To see if it helps, convert that string
manually
> into a wide char using L"" and see if that works..

> --
> Girish Bharadwaj


> > Hello,

> > We have generated an ATL COM DLL (using the wizard) in which we throw
and
> > catch exceptions (C++) and return a COM Error as follows:

> > try {
> >     int i = 20;
> >     throw i;
> > }
> > catch (int & e) {
> >     return Error("I caught an exception");
> > }

> > return S_OK;

> > This code compiles but causes access violation if we use the compiler
> > optimization to maximize speed. No problem if optimized for size.

> > The access violation occurs as soon as we return the "Error" and not at
> > throw time.

> > I was wondering if anyone could shed light on this.

> > Thanks

> > Prasenjit



Mon, 24 Mar 2003 03:00:00 GMT  
 Throwing exceptions in ATL dll with optimize for SPEED causes Access Violation
Hello,

Thanks for the reply.

My understanding of that limitation is that because _alloca allocates on the
stack and should not be called directly from a try-catch block. It is
however OK to call it from a function which is called from the try-catch
block. In this case, the call stack looks as follows:

AtlSetErrorInfo
AtlReportError        inline  calls _alloca
Error                       non-inline
Function calling error in catch block

I realize that AtlReportError is inlined away on optimize for speed, but
there is still the Error() function which would provide the necessary stack

Thanks again.

Prasenjit


Quote:
> The only thing I can think of is that Error() uses AtlReportError() which
> uses A2COLE() which uses (ultimately) A2W which uses _alloca() and that is
> not supposed to be used in a try/catch block..
> Check out KB article
> PRB: Calling String Conversion Macros in Catch Block Causes AV
> ID: Q198009 .. That explains the problem..

> That might be the problem. To see if it helps, convert that string
manually
> into a wide char using L"" and see if that works..

> --
> Girish Bharadwaj


> > Hello,

> > We have generated an ATL COM DLL (using the wizard) in which we throw
and
> > catch exceptions (C++) and return a COM Error as follows:

> > try {
> >     int i = 20;
> >     throw i;
> > }
> > catch (int & e) {
> >     return Error("I caught an exception");
> > }

> > return S_OK;

> > This code compiles but causes access violation if we use the compiler
> > optimization to maximize speed. No problem if optimized for size.

> > The access violation occurs as soon as we return the "Error" and not at
> > throw time.

> > I was wondering if anyone could shed light on this.

> > Thanks

> > Prasenjit



Mon, 24 Mar 2003 03:00:00 GMT  
 Throwing exceptions in ATL dll with optimize for SPEED causes Access Violation
I stand corrected.

The Error function is indeed in-line, which would explain the behavior.

Thanks.


Quote:
> Hello,

> Thanks for the reply.

> My understanding of that limitation is that because _alloca allocates on
the
> stack and should not be called directly from a try-catch block. It is
> however OK to call it from a function which is called from the try-catch
> block. In this case, the call stack looks as follows:

> AtlSetErrorInfo
> AtlReportError        inline  calls _alloca
> Error                       non-inline
> Function calling error in catch block

> I realize that AtlReportError is inlined away on optimize for speed, but
> there is still the Error() function which would provide the necessary
stack

> Thanks again.

> Prasenjit



> > The only thing I can think of is that Error() uses AtlReportError()
which
> > uses A2COLE() which uses (ultimately) A2W which uses _alloca() and that
is
> > not supposed to be used in a try/catch block..
> > Check out KB article
> > PRB: Calling String Conversion Macros in Catch Block Causes AV
> > ID: Q198009 .. That explains the problem..

> > That might be the problem. To see if it helps, convert that string
> manually
> > into a wide char using L"" and see if that works..

> > --
> > Girish Bharadwaj


> > > Hello,

> > > We have generated an ATL COM DLL (using the wizard) in which we throw
> and
> > > catch exceptions (C++) and return a COM Error as follows:

> > > try {
> > >     int i = 20;
> > >     throw i;
> > > }
> > > catch (int & e) {
> > >     return Error("I caught an exception");
> > > }

> > > return S_OK;

> > > This code compiles but causes access violation if we use the compiler
> > > optimization to maximize speed. No problem if optimized for size.

> > > The access violation occurs as soon as we return the "Error" and not
at
> > > throw time.

> > > I was wondering if anyone could shed light on this.

> > > Thanks

> > > Prasenjit



Mon, 24 Mar 2003 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Stepping into a class function causes exception error: access violation

2. Do access violations cause exceptions?

3. BUG: Exception causes access violation in std::string::_Tidy

4. Do access violations cause exceptions?

5. SysAllocString causes access violation (0xC0000005) exception

6. ATL COM DLL :: DllRegisterserver throw exception 0x80029c4a

7. Help! - msdev causes access violation in devshl.dll

8. PostMessage from DLL causes access violations, help please

9. MouseOver causes Access Violation (MFC42.DLL)

10. Unhandled exception in... (ODBCCR32.DLL): 0x0000005: access violation

11. unhandled exception in someprog.exe (NTDLL.DLL): 0xC0000005: Access Violation

12. unhandled exception in someprog.exe (NTDLL.DLL): 0xC0000005: Access Violation

 

 
Powered by phpBB® Forum Software