From 99e6c5a733f6d0b5ad4c13e4ed224d179794889a Mon Sep 17 00:00:00 2001 From: TautCony Date: Tue, 10 Oct 2023 17:03:29 +0800 Subject: [PATCH] fix memory leak in extensions (#229) --- .../codegpt/settings/ServiceSelectionForm.java | 9 ++++++--- .../codegpt/settings/SettingsComponent.java | 2 +- .../codegpt/settings/SettingsConfigurable.java | 15 +++++++++------ .../configuration/ConfigurationConfigurable.java | 15 +++++++++------ 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/main/java/ee/carlrobert/codegpt/settings/ServiceSelectionForm.java b/src/main/java/ee/carlrobert/codegpt/settings/ServiceSelectionForm.java index 471e38cce..cb9de5551 100644 --- a/src/main/java/ee/carlrobert/codegpt/settings/ServiceSelectionForm.java +++ b/src/main/java/ee/carlrobert/codegpt/settings/ServiceSelectionForm.java @@ -1,8 +1,8 @@ package ee.carlrobert.codegpt.settings; +import com.intellij.openapi.Disposable; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.ui.ComboBox; -import com.intellij.openapi.util.Disposer; import com.intellij.ui.TitledSeparator; import com.intellij.ui.components.JBCheckBox; import com.intellij.ui.components.JBPasswordField; @@ -32,6 +32,8 @@ public class ServiceSelectionForm { + private final Disposable parentDisposable; + private static final OpenAIChatCompletionModel[] DEFAULT_OPENAI_MODELS = new OpenAIChatCompletionModel[] { OpenAIChatCompletionModel.GPT_3_5, OpenAIChatCompletionModel.GPT_3_5_16k, @@ -67,7 +69,8 @@ public class ServiceSelectionForm { private final JPanel youServiceSectionPanel; private final JBCheckBox displayWebSearchResultsCheckBox; - public ServiceSelectionForm(SettingsState settings) { + public ServiceSelectionForm(Disposable parentDisposable, SettingsState settings) { + this.parentDisposable = parentDisposable; var openAISettings = OpenAISettingsState.getInstance(); var azureSettings = AzureSettingsState.getInstance(); openAIApiKeyField = new JBPasswordField(); @@ -233,7 +236,7 @@ private JPanel createAzureServiceSectionPanel() { private JPanel createYouServiceSectionPanel() { return FormBuilder.createFormBuilder() - .addComponent(new YouServiceSelectionPanel(Disposer.newDisposable())) + .addComponent(new YouServiceSelectionPanel(parentDisposable)) .addComponent(new TitledSeparator("Chat Preferences")) .addComponent(withEmptyLeftBorder(displayWebSearchResultsCheckBox)) .getPanel(); diff --git a/src/main/java/ee/carlrobert/codegpt/settings/SettingsComponent.java b/src/main/java/ee/carlrobert/codegpt/settings/SettingsComponent.java index 3ea6ae0d0..4db654386 100644 --- a/src/main/java/ee/carlrobert/codegpt/settings/SettingsComponent.java +++ b/src/main/java/ee/carlrobert/codegpt/settings/SettingsComponent.java @@ -18,7 +18,7 @@ public class SettingsComponent { private final YouServiceSelectionPanel youServiceSelectionPanel; public SettingsComponent(Disposable parentDisposable, SettingsState settings) { - serviceSelectionForm = new ServiceSelectionForm(settings); + serviceSelectionForm = new ServiceSelectionForm(parentDisposable, settings); displayNameField = new JBTextField(settings.getDisplayName(), 20); youServiceSelectionPanel = new YouServiceSelectionPanel(parentDisposable); mainPanel = FormBuilder.createFormBuilder() diff --git a/src/main/java/ee/carlrobert/codegpt/settings/SettingsConfigurable.java b/src/main/java/ee/carlrobert/codegpt/settings/SettingsConfigurable.java index 14bdab418..f3cb1b4d9 100644 --- a/src/main/java/ee/carlrobert/codegpt/settings/SettingsConfigurable.java +++ b/src/main/java/ee/carlrobert/codegpt/settings/SettingsConfigurable.java @@ -2,6 +2,7 @@ import com.intellij.openapi.Disposable; import com.intellij.openapi.options.Configurable; +import com.intellij.openapi.util.Disposer; import ee.carlrobert.codegpt.CodeGPTBundle; import ee.carlrobert.codegpt.conversations.ConversationsState; import ee.carlrobert.codegpt.credentials.AzureCredentialsManager; @@ -16,7 +17,9 @@ import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.Nullable; -public class SettingsConfigurable implements Configurable, Disposable { +public class SettingsConfigurable implements Configurable { + + private Disposable parentDisposable; private SettingsComponent settingsComponent; @@ -35,7 +38,8 @@ public JComponent getPreferredFocusedComponent() { @Override public JComponent createComponent() { var settings = SettingsState.getInstance(); - settingsComponent = new SettingsComponent(this, settings); + parentDisposable = Disposer.newDisposable(); + settingsComponent = new SettingsComponent(parentDisposable, settings); return settingsComponent.getPanel(); } @@ -109,13 +113,12 @@ public void reset() { @Override public void disposeUIResources() { + if (parentDisposable != null) { + Disposer.dispose(parentDisposable); + } settingsComponent = null; } - @Override - public void dispose() { - } - private boolean isServiceChanged( ServiceSelectionForm serviceSelectionForm, SettingsState settings) { diff --git a/src/main/java/ee/carlrobert/codegpt/settings/configuration/ConfigurationConfigurable.java b/src/main/java/ee/carlrobert/codegpt/settings/configuration/ConfigurationConfigurable.java index ddee25c10..16f7f7a4c 100644 --- a/src/main/java/ee/carlrobert/codegpt/settings/configuration/ConfigurationConfigurable.java +++ b/src/main/java/ee/carlrobert/codegpt/settings/configuration/ConfigurationConfigurable.java @@ -2,13 +2,16 @@ import com.intellij.openapi.Disposable; import com.intellij.openapi.options.Configurable; +import com.intellij.openapi.util.Disposer; import ee.carlrobert.codegpt.CodeGPTBundle; import ee.carlrobert.codegpt.actions.editor.EditorActionsUtil; import javax.swing.JComponent; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.Nullable; -public class ConfigurationConfigurable implements Configurable, Disposable { +public class ConfigurationConfigurable implements Configurable { + + private Disposable parentDisposable; private ConfigurationComponent configurationComponent; @@ -22,7 +25,8 @@ public String getDisplayName() { @Override public JComponent createComponent() { var configuration = ConfigurationState.getInstance(); - configurationComponent = new ConfigurationComponent(this, configuration); + parentDisposable = Disposer.newDisposable(); + configurationComponent = new ConfigurationComponent(parentDisposable, configuration); return configurationComponent.getPanel(); } @@ -60,10 +64,9 @@ public void reset() { @Override public void disposeUIResources() { + if (parentDisposable != null) { + Disposer.dispose(parentDisposable); + } configurationComponent = null; } - - @Override - public void dispose() { - } }