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 7903329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:43:17+00:00 2026-06-03T09:43:17+00:00

Trying to track down some memory leaks in our WPF/MVVM app and something occurred

  • 0

Trying to track down some memory leaks in our WPF/MVVM app and something occurred to me…

If you listen to ModelItem events in its associated ViewModel with an instance-specific handler, doesn’t that make the ViewModel hang around as long as the ModelItem still exists?

Consider this case…

public class ItemViewModel
{
    public ItemViewModel(ModelItem item)
    {
        this.Item = item;
        item.SomeEvent += ItemSomeEventHandler
    }

    Public ModelItem Item { get; private set; }

    // Note: This is a handler on this instance, not a shared handler
    private void ItemSomeEventHandler(object s, EventArgs e)
    {
        Console.WriteLine(“The model changed!”);
    }
}

If this does cause a leak, how/where do you unhook it? You can’t do it in ‘Dispose’ since that won’t be called if something still has a reference to it, which it looks like it does: the model item.

Would the proper place be to implement Dispose in the control where this ViewModel is used, then trickle down an event-unhook routine there? Seems a bit treacherous to rely on something external to make sure you clean up your own internal, but short of going to an EventManager architecture, I’m not sure what to do here.

  • 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-06-03T09:43:18+00:00Added an answer on June 3, 2026 at 9:43 am

    Yes it will cause a leak.. or more specifically the ItemViewModel will not be collected when the view is no longer displayed since the ModelItem holds a reference to the ItemViewModel

    You could implement Dispose and unhook the eventhandler which is perfectly valid if you can control the lifecycle of the ViewModel. The question is what component is creating the ItemViewModel? If it is another view model then you can delegate the dispose to that or if the view creates the view model then you can have the call the Dispose method.

    However if you use like the MVVMLight toolkit then you can use its ICleanup interface instead or something similar. Check this other answer out for more info.

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

Sidebar

Related Questions

I'm trying to use SQL Server Profiler (2005) to track down some application performance
I'm trying to track down an issue in our MFC code that looks like
I'm trying to track down a memory leak in a java process, using jmap
I've found a weird effect when trying to track down a memory leak in
I'm trying to track down a memory leak in a COM object, and I'm
I'm trying to track down a bug with some random characters appearing when saving
I'm currently trying to track down the source of some lazy loading calls in
I'm trying to track down some performance issues I have had with Django. There
I have been trying to track down some sample applications that are using Castle
I'm trying to track down a memory leak in a PHP Program (Magento, if

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.