I could add special support for such cases in the code generator. JEmalloc.free and Stdlib.free could also use it. On the other hand, all LWJGL methods have the same contract: when you pass a buffer argument, you tell LWJGL to do something with the memory address at the current buffer position. So, having to reset the position is not an unusual requirement.
I'll propose two workarounds, let me know if either is good enough for you:
- You can use the unsafe version like so: nstbi_image_free(memAddress0(imageBuffer))
The memAddress0 in MemoryUtil returns the base buffer address, ignoring the current position. It is equivalent to JNI's GetDirectBufferAddress.
- You can simply use the default allocator via MemoryUtil: memFree(imageBuffer)
This is an implementation detail, but it is guaranteed that stb uses the same allocator as LWJGL's explicit memory management API. The same is true for any other library that comes with LWJGL and has configurable allocation. The allocator can be changed with the Configuration.MEMORY_ALLOCATOR option and defaults to jemalloc. So, calling stbi_image_free is basically the same as calling memFree (which by default is the same as calling JEmalloc.free).
The memFree and memAlignedFree methods are the only methods in LWJGL that "break" the buffer contract and use memAddress0 internally, so they already do the right thing.