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內存分析器,
任何建議,以清理對象。
謝謝。
有關類似問題,請參見http://stackoverflow.com/questions/1774202/not-calling-delegate-endinvoke-can-cause-memory-leak-a-myth。只要委託和使用類共享相同的生命週期,不會有內存泄漏。 – 2012-07-30 06:48:30
如果沒有內存泄漏,不需要使用EndInvoke()? – 2012-07-30 06:57:34
你的SendCallback()看起來有點遞歸... – 2012-07-30 07:00:50