Results 1 to 2 of 2

Thread: Destroying TextControl instance

  1. #1
    Join Date
    May 2015
    Posts
    2

    Exclamation Destroying TextControl instance

    Code:
    using System;
    using System.Diagnostics;
    using System.Threading;
    using System.Windows;
    using System.Windows.Controls;
    
    namespace WPFLabor2
    {
        public partial class TextControlTest : Window
        {
            public TextControlTest()
            {
                InitializeComponent();
            }
    
            private void Create_Click(object sender, RoutedEventArgs e)
            {
                Window wnd = new Window();
                DestroyableBorder brd = new DestroyableBorder();
    
                wnd.Content = brd;
                brd.Child = new DestroyableTextControl();
    
                wnd.Show();
                wnd.Close();
    
                // brd.Child = null;
                wnd.Content = null;
                brd = null;
                wnd = null;
            }
    
            private void Destroy_Click(object sender, RoutedEventArgs e)
            {
                for (int i = 0; i < 10; i++)
                {
                    Debug.WriteLine("[" + i + "] Unloading and GC collect!");
    
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    Thread.Sleep(i * 100);
                }
            }
        }
    
        public class DestroyableBorder : Border
        {
            public DestroyableBorder() { Debug.WriteLine("Created DestroyableBorder: " + this.GetHashCode()); }
    
            ~DestroyableBorder() { Debug.WriteLine("Destroyed DestroyableBorder: " + this.GetHashCode()); }
        }
        public class DestroyableTextControl : TXTextControl.WPF.TextControl
        {
            public DestroyableTextControl() { Debug.WriteLine("Created DestroyableTextControl: " + this.GetHashCode()); }
    
            ~DestroyableTextControl() { Debug.WriteLine("Destroyed DestroyableTextControl: " + this.GetHashCode()); }
        }
    }
    Press Create, Press Destroy:

    Output:
    Code:
    Created DestroyableBorder: 28365320
    Created DestroyableTextControl: 18014196
    [0] Unloading and GC collect!
    [1] Unloading and GC collect!
    [2] Unloading and GC collect!
    [3] Unloading and GC collect!
    [4] Unloading and GC collect!
    [5] Unloading and GC collect!
    [6] Unloading and GC collect!
    [7] Unloading and GC collect!
    [8] Unloading and GC collect!
    [9] Unloading and GC collect!
    With the code above I want to ask, if there is any way to completly destroy the instance of DestroyableTextControl. It seems that TXTextControl.WPF.TextControl allocates and references some things and therefore can not be destroyed completly, even if the control itsself has been unloaded.

    If i uncomment the line // brd.Child = null, then the output is:
    Code:
    Created DestroyableBorder: 55810301
    Created DestroyableTextControl: 41440095
    Destroyed DestroyableBorder: 55810301
    [0] Unloading and GC collect!
    [1] Unloading and GC collect!
    [2] Unloading and GC collect!
    [3] Unloading and GC collect!
    [4] Unloading and GC collect!
    [5] Unloading and GC collect!
    [6] Unloading and GC collect!
    [7] Unloading and GC collect!
    [8] Unloading and GC collect!
    [9] Unloading and GC collect!
    How can I completly destroy the TextControl, because when creating a control memory is consumed and never given back, which will cause serious memory problems.
    Please notice that I force the GC to do its work, and all other things get destroyed. The answer "you can't control the gc" does not apply in this case.

  2. #2
    Join Date
    Sep 2004
    Location
    Bremen, Germany
    Posts
    5,945

    Re: Destroying TextControl instance

    Hello there,

    The actual cause is not a memory leak, but the inability to unload assemblies in .NET which have been loaded with Assembly.Load or Assembly.LoadFrom.

    The workaround for this situation is to create a new AppDomain which can be unloaded and unloads/removes all assemblies previously loaded.
    Here is a simple code snippet how to do that:

    Code:
    AppDomain dom = AppDomain.CreateDomain("TXEditor");
    
                CrossAppDomainDelegate action = () =>
                    
                        {
                            App app = new App();
                            app.MainWindow = new Window1();
                            app.MainWindow.ShowDialog();
    
                            app.Run();
    
                        };
                dom.DoCallBack(action);
                AppDomain.Unload(dom);
    Regards,
    Gunnar Giffey, TX Text Control
    [Forum Administrator]

Similar Threads

  1. Replies: 0
    Last Post: February 20, 2013, 06:21:07
  2. [SOLVED] Object Reference not set to an instance of an object while declaring Table Cell
    By phreq in forum TX Text Control .NET for Windows Forms
    Replies: 1
    Last Post: February 16, 2011, 07:10:41
  3. What slows the textcontrol with Image after second instance
    By padhu in forum TX Text Control .NET for Windows Forms
    Replies: 0
    Last Post: November 9, 2010, 16:23:40
  4. Transfer document from one control instance to another
    By tracylinne in forum TX Text Control .NET for Windows Forms
    Replies: 9
    Last Post: July 23, 2008, 01:46:04
  5. Object reference not set to an instance of an object. MS WORD
    By Henderson in forum TX Text Control .NET for Windows Forms
    Replies: 3
    Last Post: September 17, 2004, 01:02:59

Bookmarks

Posting Permissions

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