Example #1
0
        private void TestDisposeButton_Click(object sender, RoutedEventArgs e)
        {
            LogTextBox.Text = "Please wait...";
            var success = System.Threading.ThreadPool.QueueUserWorkItem(TestDispose);

            if (!success)
            {
                ProgressBarPanel.UpdateProgress("Scan failed!", "", true);
            }
        }
Example #2
0
        void TestDispose(object state)
        {
            var text = TestMemoryLeakAssemblies(
                typeof(App).Assembly,
                typeof(Engine.EngineHelper).Assembly
                );

            ControlsHelper.Invoke(() =>
            {
                ProgressBarPanel.UpdateProgress();
                LogTextBox.Text = text;
            });
        }
Example #3
0
 public DebugControl()
 {
     InitHelper.InitTimer(this, InitializeComponent);
     if (ControlsHelper.IsDesignMode(this))
     {
         return;
     }
     ProgressBarPanel.UpdateProgress();
     _TestTimer           = new HiResTimer(1, "TestTimer");
     _TestTimer.AutoReset = true;
     CpuTimer             = new System.Timers.Timer();
     CpuTimer.Interval    = 1000;
     CpuTimer.AutoReset   = false;
     CpuTimer.Elapsed    += CpuTimer_Elapsed;
     LoadSettings();
     CheckTimer();
 }
Example #4
0
        public string TestMemoryLeakAssemblies(params System.Reflection.Assembly[] assemblies)
        {
            var log           = new List <string>();
            var disposedCount = 0;
            var aliveCount    = 0;
            var errorsCount   = 0;
            var e             = new ProgressEventArgs();

            for (int a = 0; a < assemblies.Length; a++)
            {
                var assembly = assemblies[a];
                e.TopCount   = assemblies.Length;
                e.TopIndex   = a;
                e.TopData    = assemblies;
                e.TopMessage = $"Assembly: {assembly.FullName}";
                ControlsHelper.Invoke(() => ProgressBarPanel.UpdateProgress(e));
                var types = assembly.GetTypes();
                for (int t = 0; t < types.Length; t++)
                {
                    var type = types[t];
                    e.SubCount   = types.Length;
                    e.SubIndex   = t;
                    e.SubData    = types;
                    e.SubMessage = $"Type: {type.FullName}";
                    ControlsHelper.Invoke(() => ProgressBarPanel.UpdateProgress(e));
                    if (type.IsInterface)
                    {
                        continue;
                    }
                    if (!type.FullName.Contains(".Controls.") && !type.FullName.Contains(".Forms."))
                    {
                        continue;
                    }
                    ControlsHelper.Invoke(() =>
                    {
                        try
                        {
                            var o  = Activator.CreateInstance(type);
                            var wr = new WeakReference(o);
                            // Verify that the WeakReference actually points to the intended object instance.
                            if (wr.Target.Equals(o))
                            {
                                // Dispose object.
                                o = null;
                                for (int i = 0; i < 4; i++)
                                {
                                    GC.Collect();
                                    GC.WaitForPendingFinalizers();
                                    GC.WaitForFullGCComplete();
                                    GC.Collect();
                                }
                                // Note: Debug mode turns off a lot of optimizations, because compiler is trying to be helpful.
                                // Debug build can keep values rooted even if you set them to null i.e. wr.IsAlive will always return TRUE.
                                if (wr.IsAlive)
                                {
                                    log.Add($"Is Alive: {type.FullName}");
                                    aliveCount++;
                                }
                                else
                                {
                                    log.Add($"Disposed: {type.FullName}");
                                    disposedCount++;
                                }
                            }
                            else
                            {
                                log.Add($"Error: NOT same as {type.FullName}");
                                errorsCount++;
                            }
                        }
                        catch (Exception ex)
                        {
                            log.Add($"Error: {type.FullName} {ex.Message}");
                            errorsCount++;
                        }
                    });
                }
            }
            var results = $"Disposed = {disposedCount}, Alive = {aliveCount}, Errors = {errorsCount}\r\n" + string.Join("\r\n", log);

            return(results);
        }