Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BridJ Memory leak using callbacks which take pointer args #440

@rrva

Description

@rrva

There is a memory leak when using callbacks which take pointer arguments from native code to java in BridJ

When running the code below, instances of org.bridj.Pointer$OrderedPointer allocated by BridJ are never garbage collected eventually leading to out of memory. Using a callback which takes a non-pointer argument seems to work fine.

liblogger.h

typedef void (*CallbackType)(char*);
void callme(CallbackType);
void run();

liblogger.c

#include "liblogger.h"
CallbackType logger;
void run() {
    for(;;) {
        logger("hello");
    }
}
void callme(CallbackType cb) {
    logger = cb;
}

Main.java

import liblogger.*;
import org.bridj.Pointer;

public class Main {

    public static void main(String[] args) throws Exception {

        LibloggerLibrary.CallbackType callback =
            new LibloggerLibrary.CallbackType() {
                @Override
                public void apply(Pointer<Byte> charPtr) {
                }
            };

        LibloggerLibrary.callme(Pointer.pointerTo(callback));
        LibloggerLibrary.run();
    }

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions