Edit

COMException when you create a WeakReference object in a .NET Framework app

This article helps you work around the COMException exception that occurs when you create a WeakReference object in a Microsoft .NET Framework-based application.

Applies to: Microsoft .NET Framework 4.5

Original KB number: 2978463

Symptoms

Assume that you develop a .NET Framework-based application. In this application, you use the reflection API to enumerate methods in a WeakReference<T> type. Then, you call the GetFunctionPointer method on the RuntimeMethodHandle handle for the WeakReference<T>.Create function. Code that resembles the following code sample retrieves the pointer to the WeakReference<T>.Create method:

var assembly = System.Reflection.Assembly.GetAssembly(typeof(WeakReference<object>));
foreach (Type t in assembly.GetTypes())
{
    if (t.Name.StartsWith("WeakReference") && t.IsGenericType)
    {
        MethodInfo[] methods = t.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance);
        foreach (MethodInfo method in methods)
        {
            if (method.Name != "Create")
            continue;
            var ptr = method.MethodHandle.GetFunctionPointer();
        }
    }
}

In this situation, a COMException is raised if you create a WeakReference<T> object that resembles the following code sample:

WeakReference<object> wr = new WeakReference<object>(new object());

If a debugger is attached to the application process, you receive an exception and call stack that resemble the following code sample:

Exception object: 0000000102b7bde8  
Exception type: System.Runtime.InteropServices.COMException  
Message: Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))  
InnerException: \<none>  
StackTrace (generated):  
\<none>  
StackTraceString: \<none>  
HResult: 80004005  

00 0081eb80 742d5ddf KERNELBASE!RaiseException+0x48  
01 0081ec20 743eff3f clr!RaiseTheExceptionInternalOnly+0x27f  
02 0081ec50 7445a27f clr!UnwindAndContinueRethrowHelperAfterCatch+0x90  
03 0081ecb4 74132b0c clr!PreStubWorker+0x162  
04 0081ece4 73a0fd37 clr!ThePreStub+0x16  
05 0081ed08 0483039f mscorlib_ni!System.WeakReference1[[System.__Canon, mscorlib]]..ctor(System.__Canon)+0x7  

Solution

To work around this issue, don't retrieve a pointer to the WeakReference<T>.Create method. This is a private method of the WeakReference<T> class. Therefore, invoking this method might cause undefined behavior even if you get a pointer to the function.