{"action":"created","issue":{"url":"https://api.github.com/repos/jacobbo/WebEye/issues/31","repository_url":"https://api.github.com/repos/jacobbo/WebEye","labels_url":"https://api.github.com/repos/jacobbo/WebEye/issues/31/labels{/name}","comments_url":"https://api.github.com/repos/jacobbo/WebEye/issues/31/comments","events_url":"https://api.github.com/repos/jacobbo/WebEye/issues/31/events","html_url":"https://github.com/jacobbo/WebEye/issues/31","id":258034093,"node_id":"MDU6SXNzdWUyNTgwMzQwOTM=","number":31,"title":"memory leak with WebEye.Controls.Wpf.WebCameraId","user":{"login":"bouazaoui","id":17064603,"node_id":"MDQ6VXNlcjE3MDY0NjAz","avatar_url":"https://avatars2.githubusercontent.com/u/17064603?v=4","gravatar_id":"","url":"https://api.github.com/users/bouazaoui","html_url":"https://github.com/bouazaoui","followers_url":"https://api.github.com/users/bouazaoui/followers","following_url":"https://api.github.com/users/bouazaoui/following{/other_user}","gists_url":"https://api.github.com/users/bouazaoui/gists{/gist_id}","starred_url":"https://api.github.com/users/bouazaoui/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bouazaoui/subscriptions","organizations_url":"https://api.github.com/users/bouazaoui/orgs","repos_url":"https://api.github.com/users/bouazaoui/repos","events_url":"https://api.github.com/users/bouazaoui/events{/privacy}","received_events_url":"https://api.github.com/users/bouazaoui/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2017-09-15T13:10:08Z","updated_at":"2018-07-20T08:47:12Z","closed_at":null,"author_association":"NONE","body":"there is a memory leak with WebEye.Controls.Wpf.WebCameraId , i have taken the last source today.\r\n\r\nHowever there are less memory leak with the Dispatcher.ShutdownStarted event dependency\r\nthanks\r\nhttps://snag.gy/w6LG2r.jpg"},"comment":{"url":"https://api.github.com/repos/jacobbo/WebEye/issues/comments/406532904","html_url":"https://github.com/jacobbo/WebEye/issues/31#issuecomment-406532904","issue_url":"https://api.github.com/repos/jacobbo/WebEye/issues/31","id":406532904,"node_id":"MDEyOklzc3VlQ29tbWVudDQwNjUzMjkwNA==","user":{"login":"Dmitry31","id":8328042,"node_id":"MDQ6VXNlcjgzMjgwNDI=","avatar_url":"https://avatars0.githubusercontent.com/u/8328042?v=4","gravatar_id":"","url":"https://api.github.com/users/Dmitry31","html_url":"https://github.com/Dmitry31","followers_url":"https://api.github.com/users/Dmitry31/followers","following_url":"https://api.github.com/users/Dmitry31/following{/other_user}","gists_url":"https://api.github.com/users/Dmitry31/gists{/gist_id}","starred_url":"https://api.github.com/users/Dmitry31/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Dmitry31/subscriptions","organizations_url":"https://api.github.com/users/Dmitry31/orgs","repos_url":"https://api.github.com/users/Dmitry31/repos","events_url":"https://api.github.com/users/Dmitry31/events{/privacy}","received_events_url":"https://api.github.com/users/Dmitry31/received_events","type":"User","site_admin":false},"created_at":"2018-07-20T08:47:12Z","updated_at":"2018-07-20T08:47:12Z","author_association":"NONE","body":"Well actually the lifetime of WPF control should not be the lifetime of the window! The control is not disposed when it is completly detached as it is stillreferenced by the Close event of the window. Lets see this most basic VM-first exemple \r\n\r\nXAML:\r\n```\r\n<Window x:Class=\"TestingWPF.MainWindow\"\r\n        xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"\r\n        xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"\r\n        xmlns:local=\"clr-namespace:TestingWPF\"\r\n        xmlns:wec=\"clr-namespace:WebEye.Controls.Wpf;assembly=WebEye.Controls.Wpf.WebCameraControl\"\r\n        Title=\"MainWindow\" Height=\"350\" Width=\"525\">\r\n\r\n    <Window.Resources>\r\n        <DataTemplate DataType=\"{x:Type local:VM1}\">\r\n            <Rectangle Fill=\"Green\" Margin=\"10\"/>\r\n        </DataTemplate>\r\n        <DataTemplate DataType=\"{x:Type local:VM2}\">\r\n            <wec:WebCameraControl/>\r\n        </DataTemplate>\r\n    </Window.Resources>\r\n    \r\n    <Grid Background=\"#333333\">\r\n        <Grid.RowDefinitions>\r\n            <RowDefinition Height=\"*\"/>\r\n            <RowDefinition Height=\"Auto\"/>\r\n        </Grid.RowDefinitions>\r\n\r\n        <ContentControl x:Name=\"MainContainer\">\r\n            \r\n        </ContentControl>\r\n\r\n        <Button Content=\"Switch\" Click=\"OnClick\" Grid.Row=\"1\"/>\r\n    </Grid>\r\n</Window>\r\n```\r\nCode behind:\r\n\r\n```\r\nnamespace TestingWPF\r\n{\r\n    public class VM1 { }\r\n    public class VM2 { }\r\n\r\n    public partial class MainWindow : Window\r\n    {\r\n        private VM1 vm1 = new VM1();\r\n        private VM2 vm2 = new VM2();\r\n\r\n        public MainWindow()\r\n        {\r\n            InitializeComponent();\r\n        }\r\n        \r\n        private void OnClick(object sender, RoutedEventArgs e)\r\n        {\r\n            MainContainer.Content = (MainContainer.Content == vm1) ? (object)vm2 : (object)vm1;\r\n        }\r\n    }\r\n}\r\n```\r\nAll it does is to switch the content ViewModel of the content control. Each VM has it's own data template that is instanciated each time the VM is set as content. Meaning that each time vm2 is the content of the MainContainer a new instance of WebCameraControl is created.\r\n\r\nThis exemple is pretty useless I didn't even start the capture. However it illustrates how basic WPF app would work. And what should happen here is that each time the vm1 is set and WebCameraControl is not in the visual tree any more the control should be disposed but it is not because auf the Window.Closed event handler in the control.\r\n\r\nThis should be handled in one of two ways:\r\n1) Use Load to initialize everything and Unload to dispose everything this way everything will be disposed regardless of the window was closed or not. However in a scenario where the control is manually reattached to the visual tree (for exemple if instead of vm2 i used an instance of WebCameraControl) all the graph would be recreated from scratch eventually with performance issues.\r\n\r\n2) Use Load to instanciate the DirectShow stuff but only if it was not yet done before. Use Unload to stop capture if it is still running. Use the destructor ~WebCameraControl to completly dispose all DirectShow part.\r\n\r\nBy the way if you run this exemple an you add a destructor ~WebCameraControl it will only be run when the window closes and as many times as the control was instanciated.\r\n\r\nI used JetBrains dotMemory and it confirms that it's only the matter of Window.Closing event.\r\n\r\n![image](https://user-images.githubusercontent.com/8328042/42992871-27fb2c8a-8c0a-11e8-83d0-6124b7d7a5ea.png)\r\n\r\n\r\n"}}
{"actor":{"display_login":"Dmitry31"}}