BUG: MFC ActiveX Document Container Hangs During Shutdown on Windows 2000 (272256)



The information in this article applies to:

  • The Microsoft Foundation Classes (MFC), when used with:
    • the operating system: Microsoft Windows 2000
  • Microsoft Excel 2002

This article was previously published under Q272256

SYMPTOMS

A standard MFC ActiveX Document container that embeds a Microsoft Excel 2000 or 2002 spreadsheet or chart hangs during application shutdown, leaving both the MFC application and Excel running in the task list. This occurs only when the application is run on a Microsoft Windows 2000 system. The same application running on a Microsoft Windows NT 4.0, Microsoft Windows 95, or a Microsoft Windows 98 works correctly.

CAUSE

The bug is the result of a change in behavior for OLE embedded objects due to the addition of Component Object Model extension (COM+) to the version of OLE32.DLL that ships with Windows 2000. The bug occurs only under special circumstances, leaving both Excel and MFC with artificially incremented reference counts, which in turn can leave them running because both parties believe that the other is still trying to keep its connection alive.

So far, the problem has only been reproduced with MFC ActiveX Document containers and Excel. This does not mean that other containers, or other ActiveX Document servers, could not be effected.

RESOLUTION

To avoid the problem, OLE containers can forcibly disconnect their proxy/stub during shutdown, thereby allowing their reference count to fall to zero and exit normally. This also frees the running instance of the OLE server once ORPC confirms that the connection is dead (which is after three non-responsive pings spaced about two minutes apart; about six minutes total).

STATUS

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

MORE INFORMATION

Steps to Reproduce the Behavior

  1. On a Windows 2000 computer, start Visual C++ 6.0, and then create a new MFC AppWizard (exe) project. Name the project MFCAxTest.
  2. In the MFC wizard, set the project type to Multi Document Interface (MDI).
  3. In step 3, select Container, and then click to select the Active document container check box. Leave the rest of the options in their default state, and click Finish to add the new project.
  4. Press the F7 key to build the project.
  5. Press F5 to run the project.
  6. On the Edit menu, select Insert Object while the project is running. Choose Microsoft Excel Chart as a new object, and then click OK. This should activate Excel as an ActiveX Document server.
  7. On the File menu, select Exit. Do not save changes when prompted.
Note that the MFC app hides but does not exit. The debugger stays running and Excel is still in the task list. Press SHIFT+F5 in Visual C++ to stop the debugger. Excel exits some time later after ORPC detects that the container has died.

Steps to Correct the Behavior

  1. In the MFC project, open the CntrItem.cpp file and find the CMFCAxTestCntrItem::OnChange function.
  2. Just after the call to the UpdateAllViews method, add the following lines of code:
     // Explicitly release inproc handler connected to our doc site    
       if (nCode == OLE_CLOSED)
       {
          IUnknown* punk = GetInterface(&IID_IUnknown);
          if (punk) ::CoDisconnectObject(punk, 0); 
       }
    					
  3. Re-compile the MFC application and repeat steps 5-7 from "Steps to Reproduce Behavior." Note that the MFC application now exits on shutdown and the disconnected Excel eventually is freed.

Modification Type:MajorLast Reviewed:12/15/2003
Keywords:kbActiveDocs kbbug kbContainer kbpending KB272256