- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
有两种方式(据我所知)会导致 C# 中的意外内存泄漏:
IDisposable
的资源第二点我不是很明白。如果源对象的生命周期比监听器长,并且监听器在没有其他引用时不再需要事件,则使用普通的 .NET 事件会导致内存泄漏:源对象在内存中保存监听器对象应该被垃圾收集。
您能否解释一下事件如何导致 C# 中的代码发生内存泄漏,以及我如何使用弱引用和不使用弱引用来编写代码来绕过它?
最佳答案
当监听器将事件监听器附加到事件时,源对象将获得对监听器对象的引用。这意味着在分离事件处理程序或收集源对象之前,垃圾收集器无法收集监听器。
考虑以下类:
class Source
{
public event EventHandler SomeEvent;
}
class Listener
{
public Listener(Source source)
{
// attach an event listner; this adds a reference to the
// source_SomeEvent method in this instance to the invocation list
// of SomeEvent in source
source.SomeEvent += new EventHandler(source_SomeEvent);
}
void source_SomeEvent(object sender, EventArgs e)
{
// whatever
}
}
...然后是以下代码:
Source newSource = new Source();
Listener listener = new Listener(newSource);
listener = null;
即使我们将 null
分配给 listener
,它也不符合垃圾回收的条件,因为 newSource
仍然持有对事件处理程序(Listener.source_SomeEvent
)。要修复此类泄漏,重要的是在不再需要时始终分离事件监听器。
上面的示例是为了关注泄漏问题而编写的。为了修复该代码,最简单的方法可能是让 Listener
保留对 Source
的引用,以便稍后可以分离事件监听器:
class Listener
{
private Source _source;
public Listener(Source source)
{
_source = source;
// attach an event listner; this adds a reference to the
// source_SomeEvent method in this instance to the invocation list
// of SomeEvent in source
_source.SomeEvent += source_SomeEvent;
}
void source_SomeEvent(object sender, EventArgs e)
{
// whatever
}
public void Close()
{
if (_source != null)
{
// detach event handler
_source.SomeEvent -= source_SomeEvent;
_source = null;
}
}
}
然后调用代码可以发出信号表明它已使用该对象完成,这将删除 Source
对 ´Listener` 的引用;
Source newSource = new Source();
Listener listener = new Listener(newSource);
// use listener
listener.Close();
listener = null;
关于c# - 事件如何导致 C# 中的内存泄漏以及弱引用如何帮助缓解这种情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3662842/
在我早期的 R 生活中,当涉及到目录和子目录之间的分隔符时,我发现了 R 和 Windows 在不同页面上的痛苦。尽管我知道这个问题,但我仍然因为必须手动在所有反斜杠前面放置一个反斜杠或用正斜杠替换所
我正在尝试使用名为 easeInBounce 的缓动 jQuery 函数,但它不起作用。我搜索了所有相关内容,所有页面都说我必须像这里一样放置 {easing:'easeInBounce'} 但它不起
我不明白为什么使用 fgets 获取输入总是给我的程序“密码错误”。 但是,当我使用 gets() 时,比如 gets(array); 它起作用了。 预期输出:当密码错误时,打印“Wrong Pass
我刚刚给了this Hadoop tuorial在 JobTracker 中读取 Hadoop 有跟腱(单点故障)的状态: The JobTracker is a single point of fa
我有一个关于 csrf 缓解的问题。文献建议在每个页面上使用 token ,该 token 必须与任何表单一起提交 - 此 token 必须有效才能进行交易。 页面上的 token 如何防止 csrf
.NET 中是否有任何预先存在的方法来检测/防止 xpath 注入(inject)攻击? 我可以预见 2 个例子,但可能还有更多。 例如 "/Some/XPath/" + UntrustedNodeN
我已经在 http://jsfiddle.net/GxpSQ/ 上发布了一个代码有一个工作代码。单击该框,它将随机播放。 基于 hungred.com 我的代码基于此示例 http://hungred
我最近不得不迁移一个由负载均衡器和三个后端组成的基础架构。每个后端都设置了 apache2 来处理 HTTP/HTTPs 请求,并通过 AJP 协议(protocol)将它们转发到 tomcat 服务
系统的一个模块处理基于 JSON 的协议(protocol),该协议(protocol)用于传输各种数据。这导致了将近一百个小代码段,如下所示: /* * Data package Foo re
我希望有人能够帮助我理解这个问题,以及我是否需要采取任何额外步骤来保护我的应用程序。 阅读此特定漏洞,它似乎会影响符合以下条件的服务器: 从使用 HTTP 级压缩的服务器提供服务 在 HTTP 响应主
我希望能够在具有 REPR CStruct/CPointer 的类中使用双指针: typedef struct CipherContext { void *cipher;
我有一个X509Certificate2实例,并获取其PrivateKey属性,这是一个 RsaCryptoServiceProvider 。 MSDN 文档表明此 RsaCryptoServiceP
我们用于安全测试的第三方工具在 Tomcat 8 上存在慢速 HTTP POST 漏洞。我们在应用程序中有一个简单的 Spring Controller 和 JSP。 现有的 Tomcat 连接器配置
我已经设置了useHttpOnly=true在 tomcat context.xml 中,并且正在使用使用 java keytool 在 server.xml 连接器元素支持中生成的自签名证书来支持
根据 Azure 文档 github 问题 ( https://github.com/MicrosoftDocs/azure-docs/issues/75652 ) 中的 Azure 支持响应,Lin
我正在对我的 Java 代码执行 veracode 扫描,它在我的 DAO 类之一中引发以下错误 SQL 命令中使用的特殊元素的不正确中和(“SQL 注入(inject)”)CWE ID 89 但是,
我正在使用 Mongoose ODM wrapper对于 NodeJS,我担心注入(inject)攻击。假设我有以下架构: const UserSchema = new mongoose.Schema
我们有一个 jaxrs 服务,不幸的是执行了原始查询,没有准备好的语句。我们使用 ESAPI 来缓解 XSS、SQLI。如下所示: private String mitigateSQLI(String
我正在尝试减轻我们对 Poodle SSL 3.0 Fallback 的脆弱性攻击。我们的管理员已经开始禁用 SSL 以支持 TLS 用于与我们服务器的入站连接。我们还建议我们的团队在他们的网络浏览器
我在 Heroku 上有一个应用程序(免费计划),并且在从 Django 管理上传媒体文件时经常收到应用程序错误。我使用 S3 来提供静态和媒体服务。通过 django admin 直接将文件上传到
我是一名优秀的程序员,十分优秀!