BUG: Visual Studio .NET Debugger Closes When You Attach the Debugger to a Process Where the Main Thread Has Already Exited (324879)



The information in this article applies to:

  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C++ .NET (2002)
  • the operating system: Microsoft Windows XP

This article was previously published under Q324879

SYMPTOMS

On a computer that is running Microsoft Windows XP, Visual Studio .NET Debugger closes unexpectedly (crashes) when the following conditions are true:
  • The main thread of the Visual C++ .NET application has exited.
  • The application has an unclosed handle on the thread.
  • Visual Studio .NET debugger attaches to the application.
This problem does not occur if the main thread of the application is still running, or if the application is started by the Visual Studio .NET debugger.

When Visual Studio .NET Debugger closes, you receive the following error message:
Microsoft Development Environment has encountered a problem and needs to close. We are sorry for the inconvenience.
When you click Click here in this dialog box, you see the following information:
AppName: devenv.exe AppVer: 7.0.9466.0 ModName: natdbgdm.dll
ModVer: 7.0.9466.0 Offset: 000060db
When you click Debug in this dialog box, you receive the following error message:
Unhandled exception at 0x53f660db in devenv.exe: 0xC0000005:
Access violation reading location 0x00000004.

Error Signature

You may see the following error signatures when you experience this problem:

Microsoft Visual C++ 2002

App Name App Version Module Name Module Version Offset
--------------------------------------------------------------------
devenv.exe 7.0.9466.0 natdbgdm.dll 7.0.9466.0 000060DB

Microsoft Visual C++ 2003

App Name App Version Module Name Module Version Offset
--------------------------------------------------------------------
devenv.exe 7.10.3077.0 natdbgdm.dll 7.10.3077.0 00006F88

CAUSE

This problem occurs because Windows sends the wrong debug event. Windows sends a load DLL event on this thread without having sent a thread create event for the thread, or Windows does not send the create process debug event.

WORKAROUND

To work around this problem, use one of the following methods:
  • Method 1: Debug the code by launching the debugger instead of attaching the debugger to the process.
  • Method 2: Close all handles to the main thread or do not exit from the main thread when you are attaching Visual Studio .NET Debugger to the process. To do this, you can use the Sleep function of the platform SDK with the INFINITE parameter.
  • Method 3: Upgrade your operating system to Microsoft Windows Server 2003. This bug has been resolved in Windows Server 2003.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article. This bug was corrected in Windows Server 2003.

MORE INFORMATION

Steps to Reproduce the Behavior

To reproduce this problem, follow these steps:
  1. Create a Win32 Console application that contains the following sample code:
    #include <windows.h>
    #include <stdio.h>
    
    typedef struct _pTst {
    	HANDLE hEvent;
    	HANDLE hThread;
    } TINIT, *LPTINIT;
    
    unsigned long _stdcall PrintThread1(LPVOID lparam)
    {
    	int i=0;
    	LPTINIT tInfo = (LPTINIT)(lparam);
    	HANDLE hEvent = tInfo->hEvent;
    	SetEvent(hEvent);
    	do 
    	{
    		printf("\t\t\tThread1 alive : %d\n",i++);
    		Sleep(1000);
    	}
    	while(1);
    	return 1;
    }
    
    void main()
    {
    	DWORD tid;
    	HANDLE hEvent;
    	TINIT tInfo;
    	hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    	tInfo.hEvent = hEvent;
    	DuplicateHandle(GetCurrentProcess(), 
    		GetCurrentThread(), 
    		GetCurrentProcess(),
    		&tInfo.hThread,	
    		THREAD_ALL_ACCESS, 
    		FALSE, 
    		NULL);
    	CreateThread(NULL, 0, &PrintThread1, (LPVOID) &tInfo, 0,&tid);
    	WaitForSingleObject(hEvent, INFINITE);
    	printf("Ready to attach\n");
    	CloseHandle(hEvent);
    	ExitThread(0);
    }
    						
  2. Press CTRL+F5 to start the application without debugging.
  3. On the Debug menu, click Processes.
  4. Click to select the process in the list, and then click Attach.
  5. Click to select Native as the program type, and then click OK.

Modification Type:MajorLast Reviewed:12/1/2003
Keywords:kbBug kbDebug kberrmsg kbpending KB324879 kbAudDeveloper