Login  Register

Re: DirectBuffer deallocation

Posted by elect on Jan 13, 2016; 3:54pm
URL: https://forum.jogamp.org/DirectBuffer-deallocation-tp4035990p4036006.html

gouessej wrote
Use getDeclaredMethod instead of getMethod
I changed both getMethod to getDeclaredMethod but nothing changed unfortunately

protected void deallocateDirectShortBuffer(ShortBuffer directBuffer) {
//        ((DirectBuffer) directBuffer).cleaner().clean();
        try {
            if (!directBuffer.isDirect()) {
                return;
            }
            Method cleanerMethod = directBuffer.getClass().getDeclaredMethod("cleaner");
            cleanerMethod.setAccessible(true);
            Object cleaner = cleanerMethod.invoke(directBuffer);
            Method cleanMethod = cleaner.getClass().getDeclaredMethod("clean");
            cleanMethod.setAccessible(true);
            cleanMethod.invoke(cleaner);

        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
    }



gouessej wrote
The cleaner is often null, when it is no longer needed because the native resource has been released or in tons of cases, it's specific to each implementation, for example when using views on a direct byte buffer (asFloatBuffer(), ...).
I still get a null there, I tried to deallocate the direct buffer even before using it and it still returns null... but it cannot be already deallocated at that time since my sample works and relies on that data..



gouessej wrote
If you need another source of inspiration, look at com.jme3.util.BufferUtils.destroyDirectBuffer() but it's a lot less robust and less portable than my deallocator :)
Could you explain briefly why?

gouessej wrote
Hi

My class already supports short, int, float, char, long, ... direct buffers. Maybe you have found a bug. Keep in mind that my class is under GPL v2 which is a viral license.
Nice. What means gpl v2 practically? May I use in my projects?

If yes, how does it work? I just instantiate the DeallocationHelper and call DeallocationHelper.deallocate(directBuffer)?