【发布时间】:2012-07-30 06:44:54
【问题描述】:
public delegate void SendCallbackType();
public class SenderBase
{
SenderBase()
{
mySend = new SendCallbackType(SendData);
mySend.BeginInvoke(SendCallback, null);
}
void SendData()
{
// process / sending data
}
void SendCallback(IAsyncResult ar)
{
**SendCallbackType worker = (SendCallbackType)((AsyncResult)ar).AsyncDelegate;
worker.EndInvoke(ar);**
//Above code is mandatory ? Working fine without them.
mySend.BeginInvoke(SendCallback, null);
}
// Test
Dictionary<SenderBase> SenderCollection = new Dictionary();
SenderCollection.Add(new SenderBase());
SenderCollection.Remove(0);
// Add and remove seven times
对象 (SenderBase) 不是垃圾收集的。他们不断向下一代发展。
使用 RedAnts 内存分析器,
清理对象的任何建议。
谢谢。
【问题讨论】:
-
请参阅stackoverflow.com/questions/1774202/… 了解类似问题。不,只要委托和使用类共享相同的生命周期,就不存在内存泄漏。
-
如果没有内存泄漏,不需要使用 EndInvoke() 吗?
-
你的 SendCallback() 看起来有点递归......
-
根据经验数据,无需使用 EndInvoke。
标签: c# memory-leaks delegates callback