Microsoft KB Archive/315735

= BUG: Visual Studio .NET Stops Responding or Closes When Evaluating a Function That Causes an Exception =

PSS ID Number: 315735

Article Last Modified on 10/15/2002

-

The information in this article applies to:


 * Microsoft Visual Studio .NET (2002), Professional Edition
 * Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
 * Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
 * Microsoft Visual Studio .NET (2002), Academic Edition

-



This article was previously published under Q315735



SYMPTOMS
When you want to debug a native executable that consumes a managed assembly, only native debugger runs. If the managed assembly contains a method that does not catch an exception, Visual Studio .NET integrated development environment (IDE) will stop responding (hang) or close unexpectedly (crash) when you want the Watch window to evaluate an expression that calls that particular method.



CAUSE
This is a problem in Visual Studio .NET Debugger.



RESOLUTION
If you want to debug a native executable that consumes a managed assembly, run both the native debugger and the managed debugger.

Set the Debugger Type to Native or to Auto, which is the default.

The Auto type automatically detects which debuggers to run according to the following conditions:
 * If you are debugging only native code, then only the Native debugger type runs.
 * If you are debugging only managed code, then only the Managed debugger type runs.
 * If you are debugging both managed code and native code, then the Mixed debugger type runs.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



Steps to Reproduce the Behavior
To reproduce the problem, follow these steps:  In Visual Studio .NET, create a blank solution named Q315735. Add to this solution a Visual C++ .NET Active Template Library (ATL) project named NativeServer. Accept the default settings in the ATL Project Wizard. Add a new ATL Simple Object named NativeCOM to the NativeServer project. Accept the default settings in the ATL Simple Object Wizard. Add a method named CauseExceptionWithTryFinally to NativeCOM. Accept the default settings in the Add Method Wizard.</li>  Paste the following code in the CauseExceptionWithTryFinally method: STDMETHODIMP CNativeCOM::CauseExceptionWithTryFinally(void) {      int i;

__try {     i = 10; // First line in try block of CauseExceptionWithTryFinal. i = 0; // Second line in try block of CauseExceptionWithTryFinal. i = 1/i; // Exceptional code in try block of CauseExceptionWithTryFinal. i = 10; // Unreachable code in try block of CauseExceptionWithTryFinal. }   __finally { i = 10; // First line in final block of CauseExceptionWithTryFinal. i = 11; // Second line in final block of CauseExceptionWithTryFinal. printf(&quot;Divide by zero&quot;);// Third line in final block of CauseExceptionWithTryFinal. }

return S_OK; }                   </li> Add a new Win32 Project named NativeClient to Solution Q315735. Set the new project as a Console Application with ATL support in the Win32 Application Wizard. Accept the other default settings.</li>  Paste the following code in NativeClient.cpp: // NativeClient.cpp : Defines the entry point for the console application. //


 * 1) include &quot;stdafx.h&quot;
 * 2) import &quot;..\NativeServer\Debug\NativeServer.dll&quot; named_guids

int _tmain(int argc, _TCHAR* argv[]) {   CoInitialize(NULL);

int i = 10;

CoUninitialize;

return 0; }

int TestSTAException2 {   HRESULT hRes = S_OK; long lRet = 0; NativeServer::INativeCOM *pTestinterface = NULL;

hRes = CoCreateInstance(NativeServer::CLSID_CNativeCOM, NULL, CLSCTX_INPROC_SERVER,        NativeServer::IID_INativeCOM, reinterpret_cast<void**>(&pTestinterface));

if(S_OK == hRes) {

__try {           hRes = pTestinterface->raw_CauseExceptionWithTryFinally; }       __except(GetExceptionCode == EXCEPTION_INT_DIVIDE_BY_ZERO) {           printf(&quot;Exception is handled.&quot;); }       __try {           hRes = pTestinterface->raw_CauseExceptionWithTryFinally; }       __finally {           printf(&quot;Exception is handled.&quot;); // First line in finally block // on TestException2. }

pTestinterface->Release; }

return 10; }                   </li>  Set a breakpoint at the following line of code: int i = 10; </li> Set NativeClient as the StartUp Project in Solution Q315735.</li> Right-click NativeServer in Solution Explorer, and then click Properties. Set the following configurations for the NativeServer project: <ol style="list-style-type: lower-alpha;"> Click Configuration Properties, click C/C++, click General, and then click Compile As Managed: Assembly Support (/clr) from the drop-down list.</li> Click Configuration Properties, click C/C++, click General, and then click Debug Information Format: Program Database (/Zi) from the drop-down list.</li> Click Configuration Properties, click C/C++, click Code Generation, and then click Enable Minimal Rebuilt: No from the drop-down list.</li> Click Configuration Properties, click C/C++, click Code Generation, and then click Basic Runtime Checks: Default from the drop-down list.</li></ol> </li> Click OK to apply the changes.</li> Build the whole solution.</li> Press F5 to debug Solution Q315735.</li> The debugger stops at the breakpoint that you set previously.</li> Type TestSTAException2 in the Watch window, and then press ENTER.</li> You receive the following error message:

&quot;TestSTAException2 CXX0001: Error: error attempting to execute user function&quot;

</li> <li>End the debugger. Right-click NativeClient in Solution Explorer, and then click Properties. Set the following configuration for the NativeClient project: <ol style="list-style-type: lower-alpha;"> <li>Click Configuration Properties, click Debugging, and then click Debugger Type: Mixed from the drop-down list.</li> <li>Click OK to apply the change.</li></ol> </li> <li>Rebuild the whole solution.</li> <li>Press F5 to debug Solution Q315735.</li> <li>The debugger stops at the breakpoint that you set previously.</li> <li>Type TestSTAException2 in the Watch window, and then press ENTER.</li></ol>

<div class="references_section">