fix(gui): try to resolve some causes of memory leak
This commit is contained in:
@@ -437,6 +437,7 @@ public class MainWindow extends JFrame {
|
||||
LogCollector.getInstance().reset();
|
||||
wrapper.close();
|
||||
tabbedPane.closeAllTabs();
|
||||
UiUtils.resetClipboardOwner();
|
||||
System.gc();
|
||||
}
|
||||
|
||||
|
||||
@@ -373,49 +373,56 @@ public class TabbedPane extends JTabbedPane {
|
||||
jumps.reset();
|
||||
curTab = null;
|
||||
lastTab = null;
|
||||
FocusManager.reset();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Component getFocusedComp() {
|
||||
return FocusManager.isActive() ? FocusManager.focusedComp : null;
|
||||
return FocusManager.getFocusedComp();
|
||||
}
|
||||
|
||||
private static class FocusManager implements FocusListener {
|
||||
static boolean active = false;
|
||||
static FocusManager listener = new FocusManager();
|
||||
static Component focusedComp;
|
||||
private static final FocusManager INSTANCE = new FocusManager();
|
||||
private static @Nullable Component focusedComp;
|
||||
|
||||
static boolean isActive() {
|
||||
return active;
|
||||
return focusedComp != null;
|
||||
}
|
||||
|
||||
static void reset() {
|
||||
focusedComp = null;
|
||||
}
|
||||
|
||||
static Component getFocusedComp() {
|
||||
return focusedComp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void focusGained(FocusEvent e) {
|
||||
active = true;
|
||||
focusedComp = (Component) e.getSource();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void focusLost(FocusEvent e) {
|
||||
active = false;
|
||||
focusedComp = null;
|
||||
}
|
||||
|
||||
static void listen(ContentPanel pane) {
|
||||
if (pane instanceof ClassCodeContentPanel) {
|
||||
((ClassCodeContentPanel) pane).getCodeArea().addFocusListener(listener);
|
||||
((ClassCodeContentPanel) pane).getSmaliCodeArea().addFocusListener(listener);
|
||||
((ClassCodeContentPanel) pane).getCodeArea().addFocusListener(INSTANCE);
|
||||
((ClassCodeContentPanel) pane).getSmaliCodeArea().addFocusListener(INSTANCE);
|
||||
return;
|
||||
}
|
||||
if (pane instanceof AbstractCodeContentPanel) {
|
||||
((AbstractCodeContentPanel) pane).getCodeArea().addFocusListener(listener);
|
||||
((AbstractCodeContentPanel) pane).getCodeArea().addFocusListener(INSTANCE);
|
||||
return;
|
||||
}
|
||||
if (pane instanceof HtmlPanel) {
|
||||
((HtmlPanel) pane).getHtmlArea().addFocusListener(listener);
|
||||
((HtmlPanel) pane).getHtmlArea().addFocusListener(INSTANCE);
|
||||
return;
|
||||
}
|
||||
if (pane instanceof ImagePanel) {
|
||||
pane.addFocusListener(listener);
|
||||
pane.addFocusListener(INSTANCE);
|
||||
return;
|
||||
}
|
||||
// throw new JadxRuntimeException("Add the new ContentPanel to TabbedPane.FocusManager: " + pane);
|
||||
|
||||
@@ -147,6 +147,7 @@ public class SearchDialog extends CommonSearchDialog {
|
||||
if (searchDisposable != null && !searchDisposable.isDisposed()) {
|
||||
searchDisposable.dispose();
|
||||
}
|
||||
resultsModel.clear();
|
||||
removeActiveTabListener();
|
||||
super.dispose();
|
||||
}
|
||||
@@ -399,6 +400,7 @@ public class SearchDialog extends CommonSearchDialog {
|
||||
if (searchTask != null) {
|
||||
searchTask.cancel();
|
||||
searchTask.waitTask();
|
||||
searchTask = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -329,6 +329,23 @@ public class UiUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Owner field in Clipboard class can store reference to CodeArea.
|
||||
* This prevents from garbage collection whole jadx object tree and cause memory leak.
|
||||
* Trying to lost ownership by new empty selection.
|
||||
*/
|
||||
public static void resetClipboardOwner() {
|
||||
try {
|
||||
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemSelection();
|
||||
if (clipboard != null) {
|
||||
StringSelection selection = new StringSelection("");
|
||||
clipboard.setContents(selection, selection);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.error("Failed to reset clipboard owner", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static int calcProgress(ITaskProgress taskProgress) {
|
||||
return calcProgress(taskProgress.progress(), taskProgress.total());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user