WPF element host memory leak

后端 未结 3 1367
暖寄归人
暖寄归人 2021-02-04 17:35

I\'m having a strange memory leak using element host on windows forms. I have a main form, which opens another form, the one that only haves the elementhost control on it (which

3条回答
  •  故里飘歌
    2021-02-04 18:17

    I have a WPF ElementHost Object on my form and noticed a memory leak as well. What I was doing was something like this:

    VAR cnt=this.MyHostControl.Child as MyWPFControl.WPObject;
    

    and would do this in scope IE:

    Private void Myfunction()
    {
    
       VAR cnt=this.MyHostControl.Child as MyWPFControl.WPObject;
       cnt.Myproerty="Issue";
    
    }
    

    Not being a big fan of a VAR (Reminds of old COM days), I decided to create a global object like this:

    MyWPFControl.WPObject cnt = null;
    

    Then on the FormLoad_EvenT()

    I initialized the object cnt like so:

    cnt = this.MyHostControl.Child as MyWPFControl.WPObject;
    

    Now my reference looks like this:

    Private void Myfunction()
    {
    
       cnt=this.MyHostControl.Child as MyWPFControl.WPObject;
       cnt.Myproerty="Issue";
    
    }
    

    And I do not need to create a var object.

    Still testing and it looks like it maybe working. We will see over time.

提交回复
热议问题