1 year ago

#364276

test-img

Marcin

Are CancellationToken's bleeding memory

I have a larger system that sometimes needs to be shut down. To accomplish this, all my Dispatcher.Invoke() methods are invoked with one and the same CancellationToken. When the system needs to be shut down, I first cancel my one token and then shut down the system.

Recently whilst playing around with dotMemory on another matter I noticed the following:

enter image description here

here I can clearly see that one of my lambda's in my dispatcher.Invoke method (the call which itself is in my AlarmsHistoryViewModel) is holding on to my AlarmsHistoryViewModel after it's view has been closed. Investigating further my DispatcherOperation I get the following:

enter image description here

Here I can see that the operation has completed (even though the invocation count looks to be 0?? no idea what is happening here). The funny thing is that my lambda (AlarmsHistoryViewModel+<>c__DisplayClass39_1) is invoked synchronously with Dispatcher.Invoke(). Why is the CancellationTokenSource holding on to a synchronously invoked lambda that has completed? I ask, because my ViewModels seem to be lingering in memory after the views close...

c#

memory-leaks

dispatcher

cancellationtokensource

cancellation-token

0 Answers

Your Answer

Accepted video resources