コード例 #1
0
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         _fadeColor.Dispose();
         _content.Dispose();
     }
     base.Dispose(disposing);
 }
コード例 #2
0
ファイル: GuiCore.cs プロジェクト: Deltares/Riskeer
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                projectObserver.Dispose();

                foreach (PluginBase plugin in Plugins.ToArray())
                {
                    DeactivatePlugin(plugin);
                }

                isExiting = true;

                mainWindow.UnsubscribeFromGui();

                Selection = null;

                if (ViewHost != null)
                {
                    ViewHost.Dispose();
                    ViewHost.ViewClosed        -= OnViewClosed;
                    ViewHost.ActiveViewChanged -= OnActiveViewChanged;
                }

                if (currentSelectionProvider != null)
                {
                    currentSelectionProvider.SelectionChanged -= OnSelectionChanged;
                }

                if (mainWindow != null && !mainWindow.IsWindowDisposed)
                {
                    mainWindow.Dispose();
                    mainWindow = null;
                }

                if (DocumentViewController != null)
                {
                    DocumentViewController.Dispose();
                    DocumentViewController = null;
                }

                MessageWindowLogAppender.Instance.MessageWindow = null;

                RemoveLogging();
            }

            #region Prevent nasty Windows.Forms memory leak (keeps references to databinding objects / controls

            Assembly systemAssembly = typeof(Component).Assembly;
            Type     reflectTypeDescriptionProviderType =
                systemAssembly.GetType("System.ComponentModel.ReflectTypeDescriptionProvider");
            FieldInfo propertyCacheInfo = reflectTypeDescriptionProviderType.GetField("_propertyCache",
                                                                                      BindingFlags.Static |
                                                                                      BindingFlags.NonPublic);
            var propertyCache = (Hashtable)propertyCacheInfo?.GetValue(null);
            propertyCache?.Clear();

            FieldInfo extendedPropertyCacheInfo = reflectTypeDescriptionProviderType.GetField(
                "_extendedPropertyCache", BindingFlags.Static | BindingFlags.NonPublic);
            var extendedPropertyCache = extendedPropertyCacheInfo?.GetValue(null) as Hashtable;
            extendedPropertyCache?.Clear();

            FieldInfo eventCacheInfo = reflectTypeDescriptionProviderType.GetField("_eventCache",
                                                                                   BindingFlags.Static |
                                                                                   BindingFlags.NonPublic);
            var eventCache = eventCacheInfo?.GetValue(null) as Hashtable;
            eventCache?.Clear();

            FieldInfo attributeCacheInfo = reflectTypeDescriptionProviderType.GetField("_attributeCache",
                                                                                       BindingFlags.Static |
                                                                                       BindingFlags.NonPublic);
            var attributeCache = attributeCacheInfo?.GetValue(null) as Hashtable;
            attributeCache?.Clear();

            Type      typeDescriptorType = systemAssembly.GetType("System.ComponentModel.TypeDescriptor");
            FieldInfo providerTableInfo  = typeDescriptorType.GetField("_providerTable",
                                                                       BindingFlags.Static | BindingFlags.NonPublic);
            var providerTable = providerTableInfo?.GetValue(null) as Hashtable;
            providerTable?.Clear();

            FieldInfo providerTypeTableInfo = typeDescriptorType.GetField("_providerTypeTable",
                                                                          BindingFlags.Static | BindingFlags.NonPublic);
            var providerTypeTable = providerTypeTableInfo?.GetValue(null) as Hashtable;
            providerTypeTable?.Clear();

            FieldInfo defaultProvidersInfo = typeDescriptorType.GetField("_defaultProviders",
                                                                         BindingFlags.Static | BindingFlags.NonPublic);
            var defaultProviders = defaultProvidersInfo?.GetValue(null) as Hashtable;
            defaultProviders?.Clear();

            #endregion

            GC.Collect();
        }