I'm currently using SWIG to wrap a unsigned char*
as a byte[]
. I'm returning this type from C++ to the Java side of my code.
Thing is, I don't really know if the responsibility to free the memory for this array falls on the native code and / or wrap, or on the Java GC.
Right now I'm using this (don't mind the awful hack to get the size...):
%typemap(jni) unsigned char * Image::getPixels "jbyteArray"
%typemap(jtype) unsigned char * Image::getPixels "byte[]"
%typemap(jstype) unsigned char * Image::getPixels "byte[]"
%typemap(javaout) unsigned char * Image::getPixels{
return $jnicall;
}
%typemap(out) unsigned char * Image::getPixels {
//There must be a proper way than using an implicit local variable name
//from the generated cxx file...
size_t length = arg1->getBpp() * arg1->getWidth() * arg1->getHeight();
$result = jenv->NewByteArray(length);
jenv->SetByteArrayRegion($result, 0, length, (const signed char*)$1);
}
Here, the NewByteArray
looks completely in the wild, and I don't know if and where I should call a ReleaseByteArrayElements
. I found this answers but I'm not sure if that's exactly the same case here.