大网数据广告活动
Web服务器与PHP

COMException When You Use WeakReference

· 52CE 我爱测

Works around an issue in which a COMException exception occurs when you create a WeakReference object in a .NET Framework-based application.

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.

Source: Microsoft Learn / IT Pro troubleshooting by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / IT Pro troubleshooting
适用范围
See source applicability · Source date 07/08/2025
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。