Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 525693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:41:55+00:00 2026-05-13T08:41:55+00:00

I have a WPF application based on PRISM that utilizes the MVVM pattern. I

  • 0

I have a WPF application based on PRISM that utilizes the MVVM pattern.

I have noticed that occasionally my view models, views and everything connected to them will hang around long after their intended lifespan.

One leak involved subscribing to CollectionChanged on a collection belonging to an injected service, another involved not calling the Stop method on a DispatcherTimer, and yet another required a collection be cleared of it’s items.

I feel using a CompositePresentationEvent is probably preferable to subscribing to CollectionChanged, but in the other scenarios I am leaning towards implementing IDisposable and have the views call the Dispose method on the view models.

But then something needs to tell the view when to call Dispose on the view model, which gets even less attractive when the complexity of the views increase, and they start including child views.

What do you think is the best approach to handling view models, to ensure they don’t leak memory?

Thanks in advance

Ian

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:41:55+00:00Added an answer on May 13, 2026 at 8:41 am

    I can tell you that I’ve experienced 100% of the pain you have experienced. We are memory leak brothers, I think.

    Unfortunately the only thing I’ve figured out to do here is something very similar to what you are thinking.

    What we’ve done is create an attached property that a view can apply to itself to bind a handler to the ViewModel:

    <UserControl ...
                 common:LifecycleManagement.CloseHandler="{Binding CloseAction}">
    ...
    </UserControl>
    

    Then our ViewModel just has a method on it of type Action:

    public MyVM : ViewModel
    {
         public Action CloseAction
         {
              get { return CloseActionInternal; }
         }
    
         private void CloseActionInternal()
         {
              //TODO: stop timers, cleanup, etc;
         }
    }
    

    When my close method fires (we have a few ways to do this… it’s a TabControl UI with “X” on the tab headers, that kind of thing), I simply check to see if this view has registered itself with the AttachedProperty. If so, I call the method referenced there.

    It’s a pretty roundabout way of simply checking to see if the DataContext of a View is an IDisposable, but it felt better at the time. The problem with checking the DataContext is you might have sub view models that also need this control. You’d either have to make sure your viewmodels chain forward this dispose call or check all of the views in the graph and see if their datacontexts are IDisposable (ugh).

    I sort of feel like there is something missing here. There are a few other frameworks that attempt to mitigate this scenario in other ways. You might take a look at Caliburn. It has a system for handling this where a ViewModel is aware of all sub view models and this enables it to automatically chain things forward. In particular, there is an interface called ISupportCustomShutdown (I think that’s what it’s called) that helps mitigate this problem.

    The best thing I’ve done, however, is make sure and use good memory leak tools like Redgate Memory Profiler that help you visualize the object graph and find the root object. If you were able to identify that DispatchTimer issue, I imagine you are already doing this.

    Edit: I forgot one important thing. There is a potential memory leak caused by one of the event handlers in DelegateCommand. Here’s a thread about it on Codeplex that explains it. http://compositewpf.codeplex.com/WorkItem/View.aspx?WorkItemId=4065

    The latest version of the Prism (v2.1) has this fixed. (http://www.microsoft.com/downloads/details.aspx?FamilyID=387c7a59-b217-4318-ad1b-cbc2ea453f40&displaylang=en).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 313k
  • Answers 313k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think the NOCOUNT way is the way to go.… May 13, 2026 at 10:52 pm
  • Editorial Team
    Editorial Team added an answer There are two ways of doing this: Firstly the PHP… May 13, 2026 at 10:52 pm
  • Editorial Team
    Editorial Team added an answer It's a bitwise XOR (exclusive OR). It evaluates to True… May 13, 2026 at 10:52 pm

Related Questions

Hi just about to get the dev team to start looking at the next
I am currently working with the Microsoft MVVM template and find the lack of
I am currently working on a project of mine which is a MSPaint-like WPF
I have written a WPF application that I want to port to Silverlight 2.
I have a WPF, ClickOnce application that I am trying to build using the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.