Closed JosiahGoeman closed 5 months ago
This seems worth documenting, at least.
Nice find. I think this should be absolutely fixed with memSet as you proposed, or also by using memCalloc. There isn't undefined memory in java, so this behavior in jme is unexpected and can cause side effects that are very hard to debug and notice.
memCalloc is what I'd recommend... but only because the old grizzled C programmers with the horror that lurks deep in their eyes all switched to calloc decades ago...
ReflectionAllocator uses
java.nio.ByteByffer.allocateDirect()
which always initializes new buffers to all zeros.LWJGLBufferAllocator uses
org.lwjgl.system.MemoryUtil.nmemAlloc()
which does not make the same guarantee. (nmemAlloc doesn't have much documentation, just redirects to this memAlloc description)Strangely, it seems that this discrepancy only starts manifesting after buffers have been destroyed, so it isn't immediately obvious. Since the uninitialized data is overwritten immediately 99% of the time, it's not a big deal, but my understanding is that LWJGLBufferAllocator is intended to be a drop-in replacement for ReflectionAllocator.
Here's an MRE:
Possible fix could be to add a MemoryUtil.memSet() call after nmemAlloc(). I can test it out and make a PR later if y'all want.