i have memory leak when using ConcurrentQueue
:
requestObject request = xxx;
Item obj= new Item ();
obj.MessageReceived += obj_MessageReceived
I have looked implementation of the Concurrent Queue. There are cases when the queue will hold references to the object after Dequeue() was called.
Concurrent Queue uses Segments to store data. There it is a part of the TryRemove method of the segment:
// If there is no other thread taking snapshot (GetEnumerator(), ToList(), etc), reset the deleted entry to null.
// It is ok if after this conditional check m_numSnapshotTakers becomes > 0, because new snapshots won't include
// the deleted entry at m_array[lowLocal].
if (m_source.m_numSnapshotTakers <= 0)
{
m_array[lowLocal] = default(T); //release the reference to the object.
}
So when you have a different thread that enumerates the queue at the same time you dequeue an object references to the object will not be set to null.