Editor of class com.intellij.openapi.editor.impl.EditorImpl hasn't been released

Hello everyone

I try to use EditorTextField to implement a customed text filed , and everything is ok.But It will throw a exception when close a project.
I google the throwed exception info for answers,and find that I need to call EditorFactory.getInstance().releaseEditor(myEditorTextField.getEditor())  explicitly.

I tried the following methods, but it still does not work:

  • Implement Disposal interface , and release the editor in dispose() method
  • Create a new class that implement ProjectComponent , and release the editor in the projectClosed() method
  • Override the finalize() function


how to fix this ?

3 comments
Comment actions Permalink

Normally you don't need to do anything to have the EditorTextField release the editor. It handles it by itself.

However, there is a bug if you use setEnabled() to toggle editable/not editable then on each call it recreates the editor.

After that nothing you can do to release the editor, I traced it only deep enough to figure out that this is what was causing it then used code from Markdown Support plugin from https://github.com/JetBrains/intellij-plugins to do what it does to disable editing of its text editor fields, not EditorTextField, but it is the same thing:

        if (myEditor != null && !myEditor.isDisposed()) {           final boolean canEditCss = myApplyCustomCssText.isSelected();           myEditor.getDocument().setReadOnly(!canEditCss);           myEditor.getSettings().setCaretRowShown(canEditCss);           Color baseColor = myEditor.getColorsScheme().getDefaultBackground();           if (canEditCss) {             ((EditorEx) myEditor).setBackgroundColor(baseColor);           } else {             ((EditorEx) myEditor).setBackgroundColor(ColorUtil.isDark(baseColor) ?                     ColorUtil.brighter(baseColor, 1) : ColorUtil.darker(baseColor, 1));           }         }


Except in this case instead of myEditor, call the getEditor() method of the EditorTextField to get its current editor.

Solved the editor hasn't been released exception on project closing like a charm.

BTW, the function setCaretRowShown() is not available in older IDEs, For safety I would wrap it in  try/catch

                    try {                         editor.getSettings().setCaretRowShown(canEdit);                     } catch (NoSuchMethodError ignored) {                     }

0
Comment actions Permalink

Forgot to mention that while the document is in readOnly() state you cannot call setText() method of the EditorTextField.

If you do need to update the text, temporarily set document to not readOnly, call setText(), restore the document read only status.

0
Comment actions Permalink

I have fixed the bug . I tried to add a ProjectManagerListener for current project , and call EditorFactoryImpl.getInstance().releaseEditor(myEditor) inner the ProjectClosing(Poeject) method. Thx anyway:^O

0

Please sign in to leave a comment.