gpt4 book ai didi

c# - C#中个别程序集无法卸载是什么原因

转载 作者:行者123 更新时间:2023-11-30 15:40:19 25 4
gpt4 key购买 nike

在 C# 中(或者通常在 .NET 中)不能从内存中卸载单个程序集。卸载只能发生在 AppDomain 级别。

我想知道这种设计背后的原因是什么?其他语言支持此功能(我认为是 C++)

最佳答案

Here is an MSDN blog post列出一些不这样做的原因。主要问题是:

First off, you are running that code in the app domain (duh!). That means there are potentially call sites and call stacks with addresses in them that are expecting to keep working. Have you ever gotten an access violation where your EIP points to 0x???????? That is an example where someone freed up a DLL, the pages got unmapped by the memory system, and then you tried to branch to it. This typically happens in COM when you have a ref counting error and you make an interface method call. We cannot afford to be as lose with managed code. We must guarantee we know all of the code you are executing and that it is type safe and verifiable. That means explicit tracking of anything that could be using that code, including GC objects and COM interop wrappers. This tracking is handled today around an app domain boundary. Tracking it at the assembly level becomes quite expensive.

我将用更高级的语言对此进行总结:

基本上,如果您简单地删除可执行代码会出错的事情会在非托管级别出错。您可能已经编译代码指向不再存在的其他编译代码,因此您的代码将跳转到一个无效区域,并且可能包含任意数据。

这在托管代码中是 Not Acceptable ,因为事情本来就是安全的,并且有一些保证。这些保证之一是您的代码无法执行任意内存部分。

要正确处理这个问题,您必须更密切地跟踪更多的事情,这将是一个很大的开销。另一种方法是只在应用程序域边界跟踪这些东西,这就是所做的。

关于c# - C#中个别程序集无法卸载是什么原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9390670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com