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를 참조하십시오. 대리자와 사용하는 클래스가 같은 수명을 공유하는 한 메모리 누수가 없습니다. –
메모리 누수가 없으면 EndInvoke()를 사용할 필요가 없습니까? –
SendCallback()은 약간 재귀 적으로 보입니다 ... –