0

first of all sorry for my english.

I found a memory leak in my code, and I don't know what else I need to do to solve it.

I'm doing the follow in all my code. I created a UserControl to simulate a window called DragItem. this only have a XML with header and Border to attach his content. The content is another UserControl as a parameter. So, when I call a new "window" I do it as follow:

var detectionInfo = new DetectionInfoPage(_projectPage.searchControl, SelectedBookmark._Object, _projectId, SelectedBookmark);
var window = new DragItem(_mainTask, _mainTask.ContentMainTask)
{
    Name = "dragDetectionInfo",
    Title = "Detection Info",
    UserControl = detectionInfo,
    ModalWidth = 800,
    ModalHeight = 500
};
detectionInfo.Initialize();
window.Show();

detectionInfo.onDetectionSaved += (o, bookmark) =>
{
    SearchBookmarks();
};
window.OnClose += (o, b) =>
{
    detectionInfo?.Destroy();
    detectionInfo = null;
    _mainTask.ContentMainTask.Children.Remove(window);
    GC.Collect();
};

I'm trying to destroy all elements and callbacks and all inside of the content "UserControl" in OnClose callback and I remove from tree the DragItem instance ("window"), and furthermore call the garbage collector... It disapears from the visible contents and I can interact with the interface normally, but the code behind the UserControl content is still working.. and sometimes I have a backgroud tasks for example "uploading files" and this process still working, is not killed.

What else I missing? How I can KILL the UserControls inside other usercontrols in WPF??

Thanks in advance!!!!

  • Use a memory profiler to inspect which references hold your objects and prevent the GC from collecting them. And, don't ever call `GC.Collect()`. You don't need this, really. – dymanoid Nov 14 '18 at 09:49
  • What does the DragItem constructor do? `_mainTask.ContentMainTask.Children.Add`? Are you registering events to the `_projectPage.searchControl`? – Jeroen van Langen Nov 14 '18 at 09:58
  • @dymanoid can you suggest some memory profiler? I don't know how it works, but.. i don't think that I can use it.. because my code is a module that is executed inside of another application.. is like a plugin. – Riqui Pijoan Gassó Nov 14 '18 at 14:16
  • @J.vanLangen YES the constructor put the "window" in the middle of the screen and add the content to the parent. that in the sample case is "_mainTask.ContentMainTask". And no, _projectPage.searchControl is only a variable to get variables that have from a parent control. But I'm not registering events.. and the problem is the same if the UserControl constructior is without parameters. So no.. is not the issue there. :( – Riqui Pijoan Gassó Nov 14 '18 at 14:23

1 Answers1

0

You need to unregister the events, call -= for detectionInfo.onDetectionSaved when you dont need it anymore

more information here : Why and How to avoid Event Handler memory leaks?

Whiletrue
  • 551
  • 1
  • 7
  • 18
  • So, you should try memory profiler like dotTrace or ANTS Memory Profiler. Or you can also use the Diagnostic Tools of Visual Studio. Make a snapshot, search your occurence and look in "Path to Root" – Whiletrue Nov 14 '18 at 13:04
  • I'm programming a plugin that after compile is a simple dll that another application use as a "plugin". I don't know how memory profile works, but I cannot execute without compile and execute directly to the destination app(or I don't know how to do it). So, I cannot execute from my IDE (or Visual Studio), do you think that I can use the memory profiler that you suggest me? thanks! – Riqui Pijoan Gassó Nov 14 '18 at 14:29
  • Yes you can do it with dotTrace. there is a trial version of 4 actual days of use. – Whiletrue Nov 15 '18 at 08:19
  • Thanks a lot I will try! – Riqui Pijoan Gassó Nov 15 '18 at 09:48