Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

When State.Terminated does not quite clean up.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    When State.Terminated does not quite clean up.

    Just something that I found for those of us where State.Terminated leaves some EventHandlers running because the GC has not yet figured out that cleanup is needed. We end up with what looks like a memory leak, and probably is one.

    Anyhow, I started investigating and came across this: https://msdn.microsoft.com/en-us/lib...v=vs.100).aspx

    The really interesting part is the last paragraph, as it gives us an alternative way to actually attach to NT events using a Weak Event Pattern. I am beginning to get more and more impressed with this c# thing.
    One interesting aspect of the weak event pattern is that you can implement the pattern against an event that is not part of your code base. From the perspective of the source, the way that handlers are attached to its event does not differ, and is controlled by the WeakEventManager. You only need to define a WeakEventManager for that event, and then account for that event as part of the ReceiveWeakEvent logic on any prospective listener that wants to use the weak event pattern to listen to that event.
    (emphasis mine)

    #2
    Good call, thanks for sharing!

    We actually use this in the SuperDOM columns to subscribe to the MarketDepth events, rather than use a virtual OnMarketDepth() for a different reason, but also addressed the GC issue as well when closing down:

    MatthewNinjaTrader Product Management

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    662 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    376 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    110 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    575 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    580 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X