Skip to content

Commit

Permalink
8262518: SwingNode.setContent does not close previous content, result…
Browse files Browse the repository at this point in the history
…ing in memory leak

Reviewed-by: angorya, jhendrikx
  • Loading branch information
prsadhuk committed Aug 29, 2023
1 parent 53682dd commit beca88c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ public static WeakReference addRecord(Object target, DisposerRecord rec) {
return ref;
}

/**
* Unregisters a previously registered {@link DisposerRecord}, removing it
* from the list of records to dispose off when the
* original target goes out of scope
* @param ref Weak reference of the object to be removed
*/
public static void removeRecord(WeakReference ref) {
disposerInstance.records.remove(ref);
}

@Override
public void run() {
while (true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import com.sun.javafx.scene.NodeHelper;
import static javafx.stage.WindowEvent.WINDOW_HIDDEN;

import com.sun.javafx.embed.swing.DisposerRecord;
import com.sun.javafx.embed.swing.SwingNodeHelper;
import com.sun.javafx.embed.swing.SwingEvents;
import com.sun.javafx.embed.swing.newimpl.SwingNodeInteropN;
Expand Down Expand Up @@ -251,6 +252,8 @@ private ReentrantLock getPaintLock() {
private boolean grabbed; // lwframe initiated grab
private Timer deactivate; // lwFrame deactivate delay for Linux
private SwingNodeInteropN swNodeIOP;
private DisposerRecord rec;
private WeakReference disposerRecRef;

{
// To initialize the class helper at the begining each constructor of this class
Expand Down Expand Up @@ -361,7 +364,10 @@ public JComponent getContent() {
*/
private void setContentImpl(JComponent content) {
if (lwFrame != null) {
swNodeIOP.disposeFrame(lwFrame);
rec.dispose();
Disposer.removeRecord(disposerRecRef);
rec = null;
disposerRecRef = null;
lwFrame = null;
}
if (content != null) {
Expand All @@ -381,7 +387,8 @@ private void setContentImpl(JComponent content) {
swNodeIOP.setContent(lwFrame, swNodeIOP.createSwingNodeContent(content, this));
swNodeIOP.setVisible(lwFrame, true);

Disposer.addRecord(this, swNodeIOP.createSwingNodeDisposer(lwFrame));
rec = swNodeIOP.createSwingNodeDisposer(lwFrame);
disposerRecRef = Disposer.addRecord(this, rec);

if (getScene() != null) {
notifyNativeHandle(getScene().getWindow());
Expand Down

3 comments on commit beca88c

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kevinrushforth
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/tag 22+7

@openjdk
Copy link

@openjdk openjdk bot commented on beca88c Sep 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kevinrushforth The tag 22+7 was successfully created.

Please sign in to comment.