Skip to content

Commit 4b4780d

Browse files
committed
Fix setData null parameter handling and resource cleanup
1 parent a3f7284 commit 4b4780d

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

jme3-core/src/main/java/com/jme3/shader/bufferobject/BufferObject.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ public void initializeEmpty(int length) {
165165
* @param data ByteBuffer containing the data to pass
166166
*/
167167
public void setData(ByteBuffer data) {
168+
if (data == null) {
169+
if (this.data != null) {
170+
BufferUtils.destroyDirectBuffer(this.data);
171+
this.data = null;
172+
}
173+
return;
174+
}
168175
ByteBuffer source = data == this.data ? data.duplicate() : data;
169176
ByteBuffer oldData = this.data;
170177

jme3-core/src/test/java/com/jme3/shader/bufferobject/DirtyRegionsIteratorTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertFalse;
55
import static org.junit.jupiter.api.Assertions.assertNotNull;
6+
import static org.junit.jupiter.api.Assertions.assertNull;
67
import static org.junit.jupiter.api.Assertions.assertTrue;
78

89
import java.nio.ByteBuffer;
@@ -88,4 +89,26 @@ public void testSetDataHandlesSelfAlias() {
8889
assertEquals((byte) 0x33, result.get());
8990
assertEquals((byte) 0x44, result.get());
9091
}
92+
93+
@Test
94+
public void testSetDataNullClearsBuffer() {
95+
BufferObject bo = new BufferObject();
96+
ByteBuffer source = ByteBuffer.allocateDirect(4);
97+
source.put(new byte[]{1, 2, 3, 4});
98+
source.flip();
99+
bo.setData(source);
100+
assertEquals(4, bo.getData().remaining());
101+
102+
bo.setData(null);
103+
// getData() auto-allocates an empty buffer when internal data is null
104+
assertEquals(0, bo.getData().remaining());
105+
}
106+
107+
@Test
108+
public void testSetDataNullOnEmptyBufferObject() {
109+
BufferObject bo = new BufferObject();
110+
// Should not throw when internal buffer is already null
111+
bo.setData(null);
112+
assertEquals(0, bo.getData().remaining());
113+
}
91114
}

0 commit comments

Comments
 (0)