Skip to content

Commit

Permalink
Memory leak in java directors when passing byte arrays (char*, size_t)
Browse files Browse the repository at this point in the history
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 swig#386
  • Loading branch information
wsfulton committed Apr 23, 2015
1 parent 5569d91 commit ea1b6e8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.current
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ See the RELEASENOTES file for a summary of changes in each release.
Version 3.0.6 (in progress)
===========================

2015-04-23: wsfulton
[Java] Bug #386 - Memory leak fix in (char *STRING, size_t LENGTH) typemaps.

2015-04-23: vadz
[Python] Make "default" typemap work again (#330, #377).

Expand Down
3 changes: 2 additions & 1 deletion Lib/java/java.swg
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit ea1b6e8

Please sign in to comment.