BUG: Methods Calls on Interface with Unloaded Application Domain Cause Access Violation (327098)



The information in this article applies to:

  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0

This article was previously published under Q327098

SYMPTOMS

When you try to call a method on an interface with an Application Domain that has been unloaded, Access Violation in Mscorwks.dll may occur. The problem occurs with the first invocation of the method.

RESOLUTION

Do not call methods on interfaces with an assembly that has been unloaded.

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. Create a new C# Class Library with the name ClassLibrary1.

    By default, Class1.cs file is created.
  2. You can overwrite the existing code by pasting the following code in Class1.cs:
    using System;
    using System.Reflection;
    
    namespace Library1
    {
    	public interface I0
    	{
    		void DoWork();
    		void DoMoreWork();
    	}
    
    	public class DoWork : MarshalByRefObject, I0
    	{
    		void I0.DoWork() {}
    		void I0.DoMoreWork() {}
    	}
    
    	public interface IObjectServer 
    	{
    		object GetTheObject();
    		void Unload();
    	}
    
    	public class ObjectServer : IObjectServer 
    	{
    		private AppDomain m_ad;
    
    		public virtual object GetTheObject() 
    		{
    			string friendlyName;
    
    			friendlyName = "ObjectServer: " + Guid.NewGuid();
    			m_ad = AppDomain.CreateDomain(friendlyName, null);
    			return m_ad.CreateInstanceFromAndUnwrap(
    				Assembly.GetAssembly(typeof(DoWork)).Location,
    				typeof(DoWork).FullName);
    		}
    
    		public virtual void Unload() 
    		{
    			AppDomain.Unload(m_ad);
    		}
    
    		public ObjectServer() : base() 
    		{
    			m_ad = null;
    		}
    	}
    }
    					
  3. Open the Property Pages of the project.
  4. Select True in Register for COM Interop of the "Build" section under Configuration Properties.
  5. Press CTRL+SHIFT+B to build the solution.

    The ClassLibrary1.tlb file is generated as a result of assembly registration.
  6. Create a new Console Application that is written in Visual C++, and then paste the following code in the source file:
    #include <stdio.h>
    #include <comdef.h>
    #import "<Path to tlb>\ClassLibrary1.tlb" no_namespace named_guids
    
    void main(void)
    {
    	CoInitialize(NULL);
    
    	IObjectServerPtr pObjSrv;
    	CoCreateInstance(CLSID_ObjectServer, 
          NULL, 
          CLSCTX_ALL, 
          IID_IObjectServer, 
          (void **)&pObjSrv);
    
    	VARIANT var;
    	HRESULT hr = pObjSrv->raw_GetTheObject(&var);
    
    	_DoWorkPtr pWork;
    	pWork = var.pdispVal;
    
    	VariantClear(&var);
    
    	I0Ptr i0;
    	i0 = pWork;
    
    	printf("I0 DoWork\n");
    	i0->DoWork();
    
    //	i0->DoMoreWork();
    //	printf("I0 DoMoreWork\n");
    	
    	printf("AppDomain Unload\n");
    	pObjSrv->Unload();
    
    	printf("I0 DoMoreWork\n");
    	i0->DoMoreWork();
    
    	i0 = NULL;
    
    	CoUninitialize();
    }
    					
  7. Compile the code that is written in C++, and then run the .exe file.

    You receive the Access Violation that is described in the "Symptoms" section of this article.
  8. Uncomment the two lines in the C-language program code, and then check again.

    The code fails gracefully if the method has been invoked before. You receive the following error message:
    Microsoft Visual C++ Runtime Library

    Runtime Error!

    Program: MyTestApp.exe

    This application has requested the Runtime to terminate it in an unusual way.
    Please contact the application's support team for more information.

REFERENCES

For addition information about exposing .NET Framework components to COM, visit the following Microsoft Developer Network (MSDN) Web site:

Modification Type:MajorLast Reviewed:4/8/2003
Keywords:kbbug kbCOMInterop kbpending KB327098