Dnx_7
asked on
C# Memory leak with ConcurrentQueue
0 down vote favorite
i have memory leak when using ConcurrentQueue :
In the "Exited" callback, i dispose the resource :
When i read documentation, the concurrentqueue copy the reference into the queue.
Can you help me to find out where the memory leak is?
Thank you,
Kind Regards.
i have memory leak when using ConcurrentQueue :
requestObject request = xxx;
Item obj= new Item ();
obj.MessageReceived += obj_MessageReceived;
obj.Exited += obj_Exited;
request.Key = obj.Key;
obj.AddRequest(request);
_queue.TryAdd(obj.Key, obj);
In the "Exited" callback, i dispose the resource :
void LiveSphere_Exited(string key)
{
Item instance;
_queue.TryRemove(key, out instance);
Task.Factory.StartNew(() =>
{
var wait = new SpinWait();
while (instance.MessageCount > 0)
{
wait.SpinOnce();
}
}).ContinueWith((t) =>
{
if (instance != null)
{
//Cleanup resources
instance.MessageReceived -= obj_MessageReceived;
instance.Exited -= obj_Exited;
instance.Dispose();
instance = null;
}
});
When i profile the code, i still have a root referenced "Item" object but i don't know where i can dispose..., The exited method is triggered and the _queue has removed the "Item" object from the queue.When i read documentation, the concurrentqueue copy the reference into the queue.
Can you help me to find out where the memory leak is?
Thank you,
Kind Regards.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i found myself the solution
You referred to this:
I'm wondering if that means you are looking at another reference still tied to the object. For instance, are you removing an item from a tree, but still leaving parent/child references intact, or something similar?