BUG: Incomplete call stack when you debug mixed managed and unmanaged Visual C++ code (317221)



The information in this article applies to:

  • Microsoft Visual C++ .NET (2002)
  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C++ 2005 Express Edition

This article was previously published under Q317221

SYMPTOMS

When you mix managed and unmanaged Visual C++ code, the Microsoft Visual Studio integrated development environment (IDE) reports incomplete call stack information.

In certain cases, the mixed managed and unmanaged stack trace may miss a stack frame of unmanaged code that is above a stack frame of managed code. In other situations, only the managed stack trace is displayed, even if there is unmanaged code above the managed code.

This may cause a problem when you debug Visual C++ applications that contain both managed and unmanaged Visual C++ code. If you step out of a managed function, the debugger may continue to run.

CAUSE

When you call unmanaged code from managed code, the .NET runtime generates stubs and calls through the stubs. During the call, .NET pushes a transition frame object onto the thread stack.

The Visual Studio debugger cannot display the complete stack trace for any frames that do not contain enough information for the debugger to proceed up the call chain.

There are many types of transitions from managed into unmanaged code; most mixed stack traces display the correct call stack information.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to reproduce the behavior

  1. Open Visual Studio .NET or Visual Studio 2005, and then create a new Managed Visual C++ Console Application project.
  2. Copy and paste the following code:
    #using   <mscorlib.dll>
    #include <stdio.h>
    
            // managed
    void manfunc(void)
    {
    	// set a Breakpoint here
    	System::Console::WriteLine("Inside  managed function.\n");
    }
    
    #pragma unmanaged
            // unmanaged
    void unmanfunc(void)
    {
    	printf("Inside unmanaged function.\n");
    	manfunc();
    }
    #pragma unmanaged
    
            // main is managed
    void main()
    {
    	printf("Inside  managed  main function.\n");
    	unmanfunc();
    }
    					
  3. Set a breakpoint on the following line of code in the void manfunc(void) function :
    System::Console::WriteLine("Inside  managed function.\n");
  4. Compile in release configuration, and then press F5 to start the debugger. When the debugger stops in
    System::Console::WriteLine("Inside  managed function.\n");
    in the manfunc function, the Call Stack window displays the following stack trace:

    man_unman.exe!manfunc()
    man_unman.exe!main()
    man_unman.exe!mainCRTStartup()
    						

    This stack misses the call to unmanfunc.

Modification Type:MinorLast Reviewed:1/12/2006
Keywords:kbBug kbDebug kbide kbIJW kbManaged kbprb KB317221 kbAudDeveloper