我可以使用 .NET SafeHandle 类并确定地释放其中的句柄吗?

发布于 2024-10-14 11:46:41 字数 1457 浏览 2 评论 0原文

我有一个 .NET 项目,在该项目中,我在运行时根据平台位数从资源中提取一个本机代码 .DLL,其中包含我需要使用的函数。我使用 LoadLibrary、GetProcAddress 和 FreeLibrary 来管理托管代码中库的加载和使用。

使用完本机库后,我想将其删除。下面是一些伪代码,显示了当前实现的工作流程:

internal class MyClass()
{    
    string nativeLibraryPath = "C:\my\path\to\extracted\library.dll";
    IntPtr nativeLibraryHandle = IntPtr.Zero;

    public void Load()
    {
        nativeLibraryHandle = NativeMethods.LoadLibrary(nativeLibraryPath);
    }

    public void ExecuteFunction()
    {
        IntPtr functionPointer = NativeMethods.GetProcAddress(nativeLibraryHandle, "MyFunctionName");
        // assume MyManagedDelegate is defined as the correct delegate type.
        MyManagedDelegate managedFunction = Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(MyManagedDelegate)) as MyManagedDelegate;
        managedFunction("foo", "bar");
    }

    public void Unload()
    {
        NativeMethods.FreeLibrary(nativeLibraryHandle);
        File.Delete(nativeLibraryPath);
    }
}

当使用 IntPtr 类型的变量时,这可以正常工作。普遍的观点(最佳实践?)表明 SafeHandle 可能是更好的方法。但是,我的理解是,当使用 SafeHandle 时,不会确定性地调用 ReleaseHandle() 方法。也就是说,在 SafeHandle 上调用 Close() 或 Dispose() 方法仅标记该类进行垃圾回收;在收集对象之前,它不会调用 ReleaseHandle。这对我来说是个问题,因为在卸载本机代码 .DLL 之前我无法删除它。如果我在 ReleaseHandle 期间调用 FreeLibrary,我如何确定性地等待库被释放(而不诉诸黑客手段,例如调用 GC.Collect())?

编辑我已经更新了示例,以更能代表我的实际代码的结构。请注意,本示例中发布的代码并不完整。我在生产代码中包含了必要的错误检查和异常处理。

I have a .NET project in which I extract from a resource, at runtime and based on platform bit-ness, a native code .DLL which contains functions I need to use. I use LoadLibrary, GetProcAddress, and FreeLibrary to manage loading and use of the library in my managed code.

After I'm done with the native library, I'd like to delete it. Here's some pseudo-code showing the workflow as currently implemented:

internal class MyClass()
{    
    string nativeLibraryPath = "C:\my\path\to\extracted\library.dll";
    IntPtr nativeLibraryHandle = IntPtr.Zero;

    public void Load()
    {
        nativeLibraryHandle = NativeMethods.LoadLibrary(nativeLibraryPath);
    }

    public void ExecuteFunction()
    {
        IntPtr functionPointer = NativeMethods.GetProcAddress(nativeLibraryHandle, "MyFunctionName");
        // assume MyManagedDelegate is defined as the correct delegate type.
        MyManagedDelegate managedFunction = Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(MyManagedDelegate)) as MyManagedDelegate;
        managedFunction("foo", "bar");
    }

    public void Unload()
    {
        NativeMethods.FreeLibrary(nativeLibraryHandle);
        File.Delete(nativeLibraryPath);
    }
}

This works fine when using variables of type IntPtr. Prevailing wisdom (best practices?) indicate that a SafeHandle might be a better approach. However, my understanding is that when using a SafeHandle, the ReleaseHandle() method is not called deterministically. That is, calling the Close() or Dispose() method on a SafeHandle only marks the class for garbage collection; it doesn't call ReleaseHandle until the object is collected. This is a problem for me because I can't delete my native code .DLL until it's unloaded. If I call FreeLibrary during ReleaseHandle, how can I deterministically wait for the library to be released (without resorting to hackery such as calling GC.Collect())?

EDIT I've updated the example to be more representative of how my actual code is structured. Note that the code posted in this example is not complete. I am including the necessary error checking and exception handling in my production code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〆凄凉。 2024-10-21 11:46:41

不,这不准确。当您的代码忘记释放句柄时,SafeHandle 负责释放句柄。只有会不确定地发生,代码在终结器线程上运行。调用 Dispose() 具有高度确定性。

您编写的代码安全,当它发生异常时,它将永久泄漏模块句柄。您应该使用finally 块来确保释放句柄。这已经足够好了,您在这里不需要 SafeHandle,因为您将句柄保留为局部变量并且始终可以确定地释放它。

No, that's not accurate. SafeHandle takes care of releasing the handle when your code forgets to do so. Only that will happen non-deterministically, code that runs on the finalizer thread. Calling Dispose() is highly deterministic.

Your code as written is not safe, it will permanently leak the module handle when it bombs on an exception. You should use a finally block to ensure the handle is released. Which is quite good enough, you don't need SafeHandle here since you keep the handle as a local variable and can always deterministically release it.

诗酒趁年少 2024-10-21 11:46:41

你错了。
调用Close() 将释放句柄。

但是,它只会在引用计数为零时关闭句柄(这是由句柄维护的;GC 不使用引用计数)

You are incorrect.
Calling Close() will free the handle.

However, it will only close the handle when its reference count is zero (this is maintained by the handle; the GC doesn't use ref-counts)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文