I have the following situtuation on the C side I have a function that builds and returns an array of integers representing RGB values for an image,
int* pxs(Image* m){
int* colors = malloc(height * width * sizeof(int));
//fill the array
return colors;
}
On the Java side I retrieve it using,
//invoke
Pointer ptr = ref.getPointer();
int pxs[] = pointer.getIntArray(0, width*height);
//to an image
Image img = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(width, height, pxs, 0 ,width));
Then the image gets painted on a panel, from my timing doing all that takes around 50 60 ms, images are from a camera so I get a new one and paint in a loop but after a while (10 secs or so) my machine comes to an halt. I am thinking this is due to garbage collection? so I was wondering if there is a way to fix this?
You are never free
ing the colors array! Unless JNA does majic, Classic memory leak.
It may be a better idea to pass a ByteBuffer to the native function and have pxs
take a char *
to be filled with data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With