Memory leak in java directors when passing byte arrays (char*, size_t)

When passing a byte array from c++ to Java using the director feature, the generated jni code does not release a temporary byte array.

This is the typemap specified in Java.swg:

    %typemap(directorin, descriptor="[B") (char *STRING, size_t LENGTH) {
    jbyteArray jb = (jenv)->NewByteArray($2);
    (jenv)->SetByteArrayRegion(jb, 0, $2, (jbyte *)$1);
    $input = jb;
    }

    %typemap(directorargout) (char *STRING, size_t LENGTH)
    %{(jenv)->GetByteArrayRegion($input, 0, $2, (jbyte *)$1); %}

Notice that the call to NewByteArray doesn't contain a symmetric release logic as the SetByteArrayRegion/GetByteArrayRegion does.

Closes #386
This commit is contained in:
William S Fulton 2015-04-23 19:17:35 +01:00
commit ea1b6e8ed5
2 changed files with 5 additions and 1 deletions

View file

@ -1346,7 +1346,8 @@ SWIG_PROXY_CONSTRUCTOR(true, true, SWIGTYPE)
$input = jb;
}
%typemap(directorargout) (char *STRING, size_t LENGTH)
%{(jenv)->GetByteArrayRegion($input, 0, $2, (jbyte *)$1); %}
%{(jenv)->GetByteArrayRegion($input, 0, $2, (jbyte *)$1);
(jenv)->DeleteLocalRef($input);%}
%apply (char *STRING, size_t LENGTH) { (char *STRING, int LENGTH) }
/* java keywords */