|
-
Jan 24th, 2010, 10:16 PM
#1
Thread Starter
Hyperactive Member
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:
//I'm using Static Void because It is contained within a static class
//I'm creating a background worker for each void call
public static void LoadData()
{
BackgroundWorker data_worker = new BackgroundWorker();
data_worker.DoWork += new DoWorkEventHandler(data_worker_DoWork);
data_worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(data_worker_RunWorkerCompleted);
data_worker.WorkerReportsProgress = true;
data_worker.ProgressChanged += new ProgressChangedEventHandler(data_worker_ProgressChanged);
data_worker.RunWorkerAsync();
}
//Background Worker events
//**DoWork
static void data_worker_DoWork(object sender, DoWorkEventArgs e)
{
//Perform Work
}
//**Completed
static void data_worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// Should Clear Event Handlers ?
// ((BackgroundWorker)sender).DoWork -= new
// DoWorkEventHandler(data_worker_DoWork); ?
//Work Completed
}
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.
-
Jan 24th, 2010, 11:16 PM
#2
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.
-
Jan 24th, 2010, 11:35 PM
#3
Thread Starter
Hyperactive Member
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:
((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.
-
Jan 24th, 2010, 11:44 PM
#4
Re: Memory Leaks
 Originally Posted by Pac_741
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.
-
Jan 26th, 2010, 12:05 AM
#5
Thread Starter
Hyperactive Member
Re: Memory Leaks
Thanks for answering my questions.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|