From 4bc4f69c486564ae08fa0cd30df477f1770294a3 Mon Sep 17 00:00:00 2001 From: parysto Date: Mon, 16 Mar 2026 14:30:54 +0100 Subject: [PATCH] Remove reference to Spatials from TempVars and ListSort to prevent memory leaks --- jme3-core/src/main/java/com/jme3/scene/Spatial.java | 10 ++++++++-- jme3-core/src/main/java/com/jme3/util/ListSort.java | 11 +++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/jme3-core/src/main/java/com/jme3/scene/Spatial.java b/jme3-core/src/main/java/com/jme3/scene/Spatial.java index f94ed817d2..a580b18247 100644 --- a/jme3-core/src/main/java/com/jme3/scene/Spatial.java +++ b/jme3-core/src/main/java/com/jme3/scene/Spatial.java @@ -706,15 +706,21 @@ void checkDoTransformUpdate() { i++; } - vars.release(); - for (int j = i; j >= 0; j--) { rootNode = stack[j]; + // Clear stack to avoid memory leaks + // (Spatials added to the stack might get detached from their parent + // after this method has been called -> they and their OGL object will not + // get garbage collected when subsequent calls to checkDoTransformUpdate() + // require only smaller stacks) + stack[j] = null; //rootNode.worldTransform.set(rootNode.localTransform); //rootNode.worldTransform.combineWithParent(rootNode.parent.worldTransform); //rootNode.refreshFlags &= ~RF_TRANSFORM; rootNode.updateWorldTransforms(); } + + vars.release(); } } diff --git a/jme3-core/src/main/java/com/jme3/util/ListSort.java b/jme3-core/src/main/java/com/jme3/util/ListSort.java index 2ed5f9af76..c3f5ff6cd9 100644 --- a/jme3-core/src/main/java/com/jme3/util/ListSort.java +++ b/jme3-core/src/main/java/com/jme3/util/ListSort.java @@ -31,6 +31,7 @@ */ package com.jme3.util; +import java.util.Arrays; import java.util.Comparator; /** @@ -240,8 +241,14 @@ public void sort(T[] array, Comparator comparator) { } // Merge all remaining runs to complete sort - mergeForceCollapse(); - + mergeForceCollapse(); + + // Clear temporary array to avoid memory leaks + // (e.g. Geometries added to the array by GeometryList#sort() might get + // detached from their parent later -> they and their OGL object will not + // get garbage collected when subsequent sorts use only portions of the + // array) + Arrays.fill(tmpArray, null); } /**