扩展方法中的Lambda表达式:可能的内存泄漏? [英] Lambdas within Extension methods: Possible memory leak?

查看:552
本文介绍了扩展方法中的Lambda表达式:可能的内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只给一个答案,一个很简单的问题通过使用扩展方法。但是,把它写下来后,我想起你不能退订事件处理程序的lambda。

I just gave an answer to a quite simple question by using an extension method. But after writing it down i remembered that you can't unsubscribe a lambda from an event handler.

到目前为止,没有什么大问题。但如何做一个扩展方法??

So far no big problem. But how does all this behave within an extension method??

下面是我再次剪断的代码。因此,谁能赐教,如果这将导致定时器在内存挂在无数如果调用此扩展方法多次?

Below is my code snipped again. So can anyone enlighten me, if this will lead to myriads of timers hanging around in memory if you call this extension method multiple times?

我会说不,造成范围计时器的是该函数内的限制。所以,离开后没有人有这个对象的引用。我只是有点不确定,因为我们是在这里一个静态类的静态函数中。

I would say no, cause the scope of the timer is limited within this function. So after leaving it no one else has a reference to this object. I'm just a little unsure, cause we're here within a static function in a static class.

public static class LabelExtensions
{
    public static Label BlinkText(this Label label, int duration)
    {
        System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();

        timer.Interval = duration;
        timer.Tick += (sender, e) =>
            {
                timer.Stop();
                label.Font = new Font(label.Font, label.Font.Style ^ FontStyle.Bold);
            };

        label.Font = new Font(label.Font, label.Font.Style | FontStyle.Bold);
        timer.Start();

        return label;
    }
}



更新



只是为了澄清我使用 System.Windows .Forms.Timer 。因此,从你的答案似乎是特别是在使用这个定时器类仅仅是正确的选择的原因,而是做任何事情,因为我希望它在这种情况下的方式。如果试图在此情况下另一个计时器类,你可以遇到麻烦的的马修发现
此外,我发现通过的一种方式使用的WeakReference ■如果我的对象是活着与否。

Update

Just to clarify i used System.Windows.Forms.Timer. So from your answers it seems, that especially using this timer class just was the right choice cause it does anything the way as i would expect it in this case. If you try another timer class in this case you can run into trouble as Matthew found out. Also i found a way by using WeakReferences if my objects are staying alive or not.

一小觉,多一点思考后,我还做了另一个改变我的测试(下面 回答)我只是增加了一个 GC.Collect的()最后一行后设定的时间为10000启动 BLINKTEXT()几次我hitted毕竟当时我的按钮2,以获得当前状态,并强制进行垃圾回收。而且,因为它似乎所有定时器将被销毁后调用停止()方法。所以,也有垃圾收集,而我BLINKTEXT已经离开,定时器运行,则不会导致任何问题。

After a little sleep and a little more thinking, i also made another change to my tester (answer below) i just added a GC.Collect() after the last line and set the duration to 10000. After starting the BlinkText() several times i hitted all the time my button2 to get the current state and to force a garbage collection. And as it seems all the timers will be destroyed after calling the Stop() method. So also a garbage collection while my BlinkText is already left and the timer is running doesn't lead to any problems.

所以经过所有良好的反应和一点点更多的测试我可以高兴地说,它只是做什么它应该不留计时器在内存中,也没有丢掉定时器,他们完成了他们的工作之前。

So after all your good responses and a little more testing i can happily say that it just does what it should without leaving timers in the memory nor throwing away the timers before they done their work.

推荐答案

您的代码是正确的,不会泄露内存或资源,但因为你的事件处理程序停止计时器。如果您注释掉 // timer.Stop(); 它将继续闪烁,即使你做了 GC.Collect的(); 以后。

Your code is correct and will not leak memory or resources but only because you stop the Timer in the eventhandler. If you comment out the //timer.Stop(); it will keep on blinking, even when you do a GC.Collect(); later on.

定时器分配一个窗口监听WM_消息,并以某种方式由固定。当计时器停止时(启用=假),它释放的窗口。结果
中的静态扩展方法上下文没有发挥作用。

The Timer allocates a Window to listen for WM_ messages and is somehow anchored by that. When the Timer is stopped (Enabled = false), it releases that Window.
The static or extension method context does not play a role.

这篇关于扩展方法中的Lambda表达式:可能的内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆