Reputation: 333
If I have a SWIG-wrapped C function
unsigned char* f()
{
unsigned char* ptr;
bar(ptr); //set ptr
return ptr;
}
and I use array_class
or array_functions
from SWIG's carrays.i
, do their delete
functions clean up the C-allocated memory?
That is, if I use array_class
to define %array_class(int, IntArray)
and do in Java
IntArray ia = IntArray.frompointer(f());
will the pointer returned by f() be freed when ia
goes out of scope or, if using array_functions
, when I explicitly invoke delete()
?
The source code for array_class
shows that the wrapped array class has a destructor:
~NAME() {
delete [] self;
}
and array_functions
has
static void delete_##NAME(TYPE *ary) { %}
%{ delete [] ary; %}
which leads me to believe it does, but the documentation doesn't say so and many examples I have seen use JNI code to create a copy of the array passed out by the C function, delete the original array, and return the copy back to Java. So I am unsure, with all the wrapping going on, what exactly these delete
functions actually delete.
Upvotes: 1
Views: 195
Reputation: 333
I discovered that the SWIG-generated array class's SwigOwn status that determines whether it owns the native memory is set to false. If you change this to true then doing myArray.delete() will indeed free the native memory it acquired by, for example,
myArray ma = myArray.frompointer(new SWIGTYPE_p_char(function_that_returns_native_ptr))
Upvotes: 1