简体   繁体   English

使用 UnityActions 或 C# Actions 时如何最好地避免内存泄漏

[英]How to best avoid memoryleaks when using UnityActions or C# Actions

Ok, I am working with public static UnityActions for event driven multi delegate execution and I want to avoid memory leaks.好的,我正在使用公共 static UnityActions 进行事件驱动的多委托执行,我想避免 memory 泄漏。 I am worried that I miss removing a registered delegate.我担心我会错过删除已注册代表的情况。 So I came up with the following construct: Immediately when registering for an event I also register the command for unregistering to a separate action that I can execute at the end of the program like so:所以我想出了以下结构:在注册事件时,我还立即注册用于取消注册的命令到一个单独的操作,我可以在程序结束时执行该操作,如下所示:

// first establish a delegate to collect everything we need to de-register later OnDestroy
private delegate void Dreg();
private Dreg dreg;

// register for the event and create an entry in the de-register list 
//for later execution:
            MyEvents.SomeEvent += HandleSomeEvent;
            dreg += () => { MyEvents.SomeEvent -= HandleSomeEvent; };

At the end of the application I just need to invoke "dreg" to clear all registrations.在应用程序结束时,我只需要调用“dreg”来清除所有注册。 This si working fine so far.到目前为止,这个 si 工作正常。

My question is: Can I wrap these two statements more elegantly into a function so that I just have to issue one statement instead of two?我的问题是:我可以将这两个语句更优雅地包装到 function 中,这样我只需要发出一个语句而不是两个语句吗? I have tried but I fail in the assignments.我试过了,但我的作业失败了。 My experiment looked something like this:我的实验看起来像这样:

public static void RegisterForEvent(UnityAction _eventToRegisterFor, UnityAction _thingToAdd)
{
    _eventToRegisterFor += _thingToAdd;
    dreg += () => _eventToRegisterFor -= _delegateToAdd;
}
...
...Register(MyEvents.SomeEvent,HandleSomeEvent);// using this to register

...
...dreg.invoke;//near the end of the application to de-register everything

This does not seem to be possible.这似乎是不可能的。 My problem seems to be that I don't quite understand, what type "thingToAdd" actually is that I am assigning to MyEvents.SomeEvent?我的问题似乎是我不太明白,我分配给 MyEvents.SomeEvent 的“thingToAdd”实际上是什么类型? Is that a delegate?是代表吗? Is it a string that is resolved at runtime?它是在运行时解析的字符串吗? is it a function??是 function 吗? I tried to work with C# Actions as well but did not succeed with that either.我也尝试使用 C# 操作,但也没有成功。 Any suggestions will be much appreciated: Many thx before hand :-)任何建议都将不胜感激:很多谢谢 :-)

Here is the way I solved this:这是我解决这个问题的方法:

//---------------------------------------------------------------------------------------------    
public static void RegisterForEvent(UnityAction _eventToRegisterFor, UnityAction _handlerToAdd)
{
    _eventToRegisterFor += _handlerToAdd;
    dreg += () => _eventToRegisterFor -= _handlerToAdd;
}

//---------------------------------------------------------------------------------------------
//...the two lines above could be replaced with this one line call...

RegisterForEvent(MyEvents.SomeEvent,HandlerForSomeEvent);

//---------------------------------------------------------------------------------------------
//...and at the end the invokation of "dreg" will clear the event delegates
private void OnDestroy()
{
    dreg.invoke;//near the end of the application to de-register everything
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM