Microsoft KB Archive/327098

= BUG: Methods Calls on Interface with Unloaded Application Domain Cause Access Violation =

Article ID: 327098

Article Last Modified on 5/16/2007

-

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.



Steps to Reproduce the Behavior
 Create a new C# Class Library with the name ClassLibrary1.

By default, Class1.cs file is created.  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 = &quot;ObjectServer: &quot; + 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; }   } }                     Open the Property Pages of the project. Select True in Register for COM Interop of the &quot;Build&quot; section under Configuration Properties. Press CTRL+SHIFT+B to build the solution.

The ClassLibrary1.tlb file is generated as a result of assembly registration.  Create a new Console Application that is written in Visual C++, and then paste the following code in the source file:
 * 1) include <stdio.h>
 * 2) include <comdef.h>
 * 3) import &quot;<Path to tlb>\ClassLibrary1.tlb&quot; 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(&quot;I0 DoWork\n&quot;); i0->DoWork;

// i0->DoMoreWork; // printf(&quot;I0 DoMoreWork\n&quot;); printf(&quot;AppDomain Unload\n&quot;); pObjSrv->Unload;

printf(&quot;I0 DoMoreWork\n&quot;); i0->DoMoreWork;

i0 = NULL;

CoUninitialize; }                   </li> Compile the code that is written in C++, and then run the .exe file.

You receive the Access Violation that is described in the &quot;Symptoms&quot; section of this article.</li> 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.

</li></ol>

<div class="references_section">