Electrostat-Lab/jme-alloc

[Incompatibility] ClassCastException when used in JME

Ali-RS opened this issue · 2 comments

Ali-RS commented
Mar 24, 2023 5:16:29 PM com.jme3.system.JmeDesktopSystem initialize
INFO: Running on jMonkeyEngine 3.6.0-stable
 * Branch: HEAD
 * Git Hash: 53f2a49
 * Build Date: 2023-03-20
Mar 24, 2023 5:16:31 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: LWJGL 2.9.5 context running on thread jME3 Main
 * Graphics Adapter: null
 * Driver Version: null
 * Scaling Factor: 1
Mar 24, 2023 5:16:31 PM com.jme3.renderer.opengl.GLRenderer loadCapabilitiesCommon
INFO: OpenGL Renderer Information
 * Vendor: Intel Open Source Technology Center
 * Renderer: Mesa DRI Intel(R) HD Graphics 3000 (SNB GT2)
 * OpenGL Version: 3.3 (Core Profile) Mesa 20.0.8
 * GLSL Version: 3.30
 * Profile: Core
Mar 24, 2023 5:16:32 PM com.jme3.audio.openal.ALAudioRenderer initOpenAL
INFO: Audio Renderer Information
 * Device: OpenAL Soft
 * Vendor: OpenAL Community
 * Renderer: OpenAL Soft
 * Version: 1.1 ALSOFT 1.15.1
 * Supported channels: 64
 * ALC extensions: ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_DEDICATED ALC_EXT_disconnect ALC_EXT_EFX ALC_EXT_thread_local_context ALC_SOFT_loopback
 * AL extensions: AL_EXT_ALAW AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_MULAW AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET AL_EXT_source_distance_model AL_LOKI_quadriphonic AL_SOFT_buffer_samples AL_SOFT_buffer_sub_data AL_SOFTX_deferred_updates AL_SOFT_direct_channels AL_SOFT_loop_points AL_SOFT_source_latency
Mar 24, 2023 5:16:32 PM com.jme3.audio.openal.ALAudioRenderer initOpenAL
WARNING: Pausing audio device not supported.
Mar 24, 2023 5:16:32 PM com.jme3.audio.openal.ALAudioRenderer initOpenAL
INFO: Audio effect extension version: 1.0
Mar 24, 2023 5:16:32 PM com.jme3.audio.openal.ALAudioRenderer initOpenAL
INFO: Audio max auxiliary sends: 4
Allocator=buffer.TestReleaseDirectMemory$JmeNativeAlloc
Mar 24, 2023 5:16:32 PM com.jme3.app.LegacyApplication handleError
SEVERE: Uncaught exception thrown in Thread[#33,jME3 Main,5,main]
java.lang.ClassCastException: class java.nio.DirectFloatBufferU cannot be cast to class java.nio.ByteBuffer (java.nio.DirectFloatBufferU and java.nio.ByteBuffer are in module java.base of loader 'bootstrap')
	at buffer.TestReleaseDirectMemory$JmeNativeAlloc.destroyDirectBuffer(TestReleaseDirectMemory.java:80)
	at com.jme3.util.BufferUtils.destroyDirectBuffer(BufferUtils.java:1292)
	at buffer.TestReleaseDirectMemory.simpleUpdate(TestReleaseDirectMemory.java:73)
	at com.jme3.app.SimpleApplication.update(SimpleApplication.java:261)
	at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:160)
	at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:224)
	at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:242)
	at java.base/java.lang.Thread.run(Thread.java:1589)

Here is a test case:

public class TestReleaseDirectMemory extends SimpleApplication {

    public static void main(String[] args){
        System.setProperty(BufferAllocatorFactory.PROPERTY_BUFFER_ALLOCATOR_IMPLEMENTATION, JmeNativeAlloc.class.getName());
        TestReleaseDirectMemory app = new TestReleaseDirectMemory();
        app.start();
    }

    @Override
    public void simpleInitApp() {
  
        System.out.println("Allocator=" + System.getProperty(BufferAllocatorFactory.PROPERTY_BUFFER_ALLOCATOR_IMPLEMENTATION));
       
    }

    @Override
    public void simpleUpdate(float tpf) {
        ByteBuffer buf = BufferUtils.createByteBuffer(500000);
        BufferUtils.destroyDirectBuffer(buf);
        
        FloatBuffer buf2 = BufferUtils.createFloatBuffer(500000);
        BufferUtils.destroyDirectBuffer(buf2);
    }

    public static class JmeNativeAlloc implements BufferAllocator {

        @Override
        public void destroyDirectBuffer(Buffer toBeDestroyed) {
            NativeBufferAllocator.releaseDirectByteBuffer((ByteBuffer) toBeDestroyed);
        }

        @Override
        public ByteBuffer allocate(int size) {
            return NativeBufferAllocator.createDirectByteBuffer(size);
        }
    }
}
Ali-RS commented

I also suggest renaming releaseDirectByteBuffer() to releaseDirectBuffer() and createDirectByteBuffer() to allocate().

@Ali-RS Feel free to review PR #49.