Skip to content

Commit

Permalink
fix memory leak in extensions (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
tautcony authored Oct 10, 2023
1 parent 7dfe62b commit 99e6c5a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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();
}

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
}

Expand Down Expand Up @@ -60,10 +64,9 @@ public void reset() {

@Override
public void disposeUIResources() {
if (parentDisposable != null) {
Disposer.dispose(parentDisposable);
}
configurationComponent = null;
}

@Override
public void dispose() {
}
}

0 comments on commit 99e6c5a

Please sign in to comment.