Results 1 to 5 of 5

Thread: Memory Leaks

  1. #1

    Thread Starter
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    Memory Leaks

    Memory Management is know issue while using WPF, but I've read workarounds for this problems.

    In my case I'm struggling with Events, I want to make sure the following piece of code is creating, and eliminating the event handlers.

    I'm creating and using a BackgroundWorker to perform some actions.

    Here's the code, I'll place the explanation after it.

    csharp Code:
    1. //I'm using Static Void because It is contained within a static class
    2. //I'm creating a background worker for each void call
    3. public static void LoadData()
    4. {
    5.      BackgroundWorker data_worker = new BackgroundWorker();
    6.      data_worker.DoWork += new DoWorkEventHandler(data_worker_DoWork);
    7.      data_worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(data_worker_RunWorkerCompleted);
    8.      data_worker.WorkerReportsProgress = true;
    9.      data_worker.ProgressChanged += new ProgressChangedEventHandler(data_worker_ProgressChanged);
    10.      data_worker.RunWorkerAsync();
    11. }
    12.  
    13. //Background Worker events
    14.  
    15. //**DoWork
    16. static void data_worker_DoWork(object sender, DoWorkEventArgs e)
    17. {
    18.      //Perform Work
    19. }
    20. //**Completed
    21.  static void data_worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    22. {
    23.  
    24.      // Should Clear Event Handlers ?
    25.      // ((BackgroundWorker)sender).DoWork -= new
    26.      // DoWorkEventHandler(data_worker_DoWork); ?
    27.      //Work Completed
    28. }

    Is this code creating the background worker in a way it will be destroyed after it's work has been performed ? If not what should I do to make it so.
    C# and WPF developer
    My Website:
    http://singlebits.com/

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Memory Leaks

    That's fine. You can simply copy and paste the code that adds the event handlers and change the += to -=.

    I'm not really sure what that has to do with WPF though. I see nothing specific to WPF there. Removing event handlers that were manually added is something that's required in all C# code. If you don't then you leave a delegate retaining a reference to the the object whose event it was handling, which prevents the object being marked for garbage collection. Removing the event handlers is, in this sense, the same as setting a variable to null.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    Re: Memory Leaks

    jmcilhinney, thanks for your response. I posted this in the WPF section because I'm using this code under a WPF app.

    So, your suggestion should end up being like this : ?

    csharp Code:
    1. ((BackgroundWorker)sender).DoWork -= new DoWorkEventHandler(data_worker_DoWork);

    Writing that code within the Completed event should remove event handlers of the background worker ? By doing so, will the background worker get collected by the GC ?

    Thanks for your response.
    C# and WPF developer
    My Website:
    http://singlebits.com/

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Memory Leaks

    Quote Originally Posted by Pac_741 View Post
    By doing so, will the background worker get collected by the GC ?
    When an object actually gets cleaned up by the GC is up to the GC. By removing all references to an object you make it available for garbage collection. If and when the object gets cleaned up depends on system activity and memory demands. If the memory occupied by the object isn't required then it may never be reclaimed, at least until the app exits. What you're doing ensures that it CAN be reclaimed, which is all you're responsible for.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    Re: Memory Leaks

    Thanks for answering my questions.
    C# and WPF developer
    My Website:
    http://singlebits.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width