Skip to content

Commit 9c6445e

Browse files
committed
Fix memory leaks in render context
1 parent 52ffaef commit 9c6445e

3 files changed

Lines changed: 23 additions & 12 deletions

File tree

jme3-core/src/main/java/com/jme3/renderer/RenderContext.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
*/
3232
package com.jme3.renderer;
3333

34+
import java.lang.ref.WeakReference;
35+
3436
import com.jme3.material.RenderState;
3537
import com.jme3.math.ColorRGBA;
3638
import com.jme3.scene.Mesh;
@@ -219,7 +221,7 @@ public class RenderContext {
219221
*
220222
* @see Renderer#setTexture(int, com.jme3.texture.Texture)
221223
*/
222-
public final Image[] boundTextures = new Image[16];
224+
public final WeakReference<Image> boundTextures[] = new WeakReference[16];
223225

224226
/**
225227
* IDList for texture units
@@ -252,7 +254,7 @@ public class RenderContext {
252254
* Vertex attribs currently bound and enabled. If a slot is null, then
253255
* it is disabled.
254256
*/
255-
public final VertexBuffer[] boundAttribs = new VertexBuffer[16];
257+
public final WeakReference<VertexBuffer> [] boundAttribs = new WeakReference[16];
256258

257259
/**
258260
* IDList for vertex attributes

jme3-core/src/main/java/com/jme3/renderer/opengl/GLRenderer.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import com.jme3.util.NativeObjectManager;
6363
import jme3tools.shader.ShaderDebug;
6464

65+
import java.lang.ref.WeakReference;
6566
import java.nio.ByteBuffer;
6667
import java.nio.FloatBuffer;
6768
import java.nio.IntBuffer;
@@ -2385,9 +2386,9 @@ private void bindTextureAndUnit(int target, Image img, int unit) {
23852386
gl.glActiveTexture(GL.GL_TEXTURE0 + unit);
23862387
context.boundTextureUnit = unit;
23872388
}
2388-
if (context.boundTextures[unit] != img) {
2389+
if (context.boundTextures[unit]==null||context.boundTextures[unit].get() != img.getWeakRef().get()) {
23892390
gl.glBindTexture(target, img.getId());
2390-
context.boundTextures[unit] = img;
2391+
context.boundTextures[unit] = img.getWeakRef();
23912392
statistics.onTextureUse(img, true);
23922393
} else {
23932394
statistics.onTextureUse(img, false);
@@ -2403,13 +2404,13 @@ private void bindTextureAndUnit(int target, Image img, int unit) {
24032404
* @param unit At what unit to bind the texture.
24042405
*/
24052406
private void bindTextureOnly(int target, Image img, int unit) {
2406-
if (context.boundTextures[unit] != img) {
2407+
if (context.boundTextures[unit] == null || context.boundTextures[unit].get() != img.getWeakRef().get()) {
24072408
if (context.boundTextureUnit != unit) {
24082409
gl.glActiveTexture(GL.GL_TEXTURE0 + unit);
24092410
context.boundTextureUnit = unit;
24102411
}
24112412
gl.glBindTexture(target, img.getId());
2412-
context.boundTextures[unit] = img;
2413+
context.boundTextures[unit] = img.getWeakRef();
24132414
statistics.onTextureUse(img, true);
24142415
} else {
24152416
statistics.onTextureUse(img, false);
@@ -2824,7 +2825,7 @@ public void clearVertexAttribs() {
28242825
for (int i = 0; i < attribList.oldLen; i++) {
28252826
int idx = attribList.oldList[i];
28262827
gl.glDisableVertexAttribArray(idx);
2827-
if (context.boundAttribs[idx].isInstanced()) {
2828+
if (context.boundAttribs[idx].get().isInstanced()) {
28282829
glext.glVertexAttribDivisorARB(idx, 0);
28292830
}
28302831
context.boundAttribs[idx] = null;
@@ -2879,13 +2880,13 @@ public void setVertexAttrib(VertexBuffer vb, VertexBuffer idb) {
28792880
updateBufferData(vb);
28802881
}
28812882

2882-
VertexBuffer[] attribs = context.boundAttribs;
2883+
WeakReference<VertexBuffer>[] attribs = context.boundAttribs;
28832884
for (int i = 0; i < slotsRequired; i++) {
28842885
if (!context.attribIndexList.moveToNew(loc + i)) {
28852886
gl.glEnableVertexAttribArray(loc + i);
28862887
}
28872888
}
2888-
if (attribs[loc] != vb) {
2889+
if (attribs[loc]==null||attribs[loc].get() != vb) {
28892890
// NOTE: Use id from interleaved buffer if specified
28902891
int bufId = idb != null ? idb.getId() : vb.getId();
28912892
assert bufId != -1;
@@ -2925,14 +2926,14 @@ public void setVertexAttrib(VertexBuffer vb, VertexBuffer idb) {
29252926

29262927
for (int i = 0; i < slotsRequired; i++) {
29272928
int slot = loc + i;
2928-
if (vb.isInstanced() && (attribs[slot] == null || !attribs[slot].isInstanced())) {
2929+
if (vb.isInstanced() && (attribs[slot] == null || attribs[slot].get() == null || !attribs[slot].get().isInstanced())) {
29292930
// non-instanced -> instanced
29302931
glext.glVertexAttribDivisorARB(slot, vb.getInstanceSpan());
2931-
} else if (!vb.isInstanced() && attribs[slot] != null && attribs[slot].isInstanced()) {
2932+
} else if (!vb.isInstanced() && attribs[slot] != null && attribs[slot].get() != null && attribs[slot].get().isInstanced()) {
29322933
// instanced -> non-instanced
29332934
glext.glVertexAttribDivisorARB(slot, 0);
29342935
}
2935-
attribs[slot] = vb;
2936+
attribs[slot] = vb.getWeakRef();
29362937
}
29372938
}
29382939
}

jme3-core/src/main/java/com/jme3/util/NativeObject.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
*/
3232
package com.jme3.util;
3333

34+
import java.lang.ref.WeakReference;
3435
import java.nio.Buffer;
3536

3637
/**
@@ -227,4 +228,11 @@ public void dispose() {
227228
objectManager.enqueueUnusedObject(this);
228229
}
229230
}
231+
232+
private WeakReference<NativeObject> weakRef;
233+
234+
public <T> WeakReference<T> getWeakRef() {
235+
if (weakRef == null) weakRef = new WeakReference<NativeObject>(this);
236+
return (WeakReference<T>) weakRef;
237+
}
230238
}

0 commit comments

Comments
 (0)