#1323 Java/up-call/NewStringUTF/DeleteLocalRef mem leak

open
nobody
None
5
2015-01-20
2013-04-29
No

Hi

Please consider the described below patch for memory leak problem that we observe since version 2.0.1 in Windows/Java.
It is not fixed in 2.0.9. The problem is that SWIG failed to generate DeleteLocalRef call for each NewStringUTF. It happens
when up calls get fired in director. I am attaching source code of the modified file java.cxx. The code snipped look like this:

String *jstrings2del = NewString("");

....

/* ykh path - create list of parameters for DeleteLocalRef */
char* tmp_param_type = (char*)DohData(c_param_type);
if(tmp_param_type != NULL && strcmp(tmp_param_type, "jstring") == 0)
  {
    char* tmp_arg = (char*)DohData(arg);
    if(tmp_arg != NULL)
      {
    Printf(jstrings2del, "jenv->DeleteLocalRef(%s);\n", tmp_arg);
      }
  }

.....

  Printf(w->code, "%s", jstrings2del);

.....

Delete(jstrings2del);

Regards,
Yuriy Khodak

1 Attachments

Discussion

  • William Fulton

    William Fulton - 2013-05-14

    What is the input code that causes the leak?

     
  • Will Stampley

    Will Stampley - 2014-05-22

    It's easy to create a very simple example that produces the problem mentioned in the bug report -

    In project_swig_settings.i

    %include std_string.i
    %feature("director") client::BaseDebugEventListener;
    %include "../src/BaseDebugEventListener.h"

    where BaseDebugEventListener has a single function destined for upcall:

    virtual void OnDebugEvent(const std::string& message) {}

    The resulting C++ wrapper has:

    jmessage = jenv->NewStringUTF((&message)->c_str());

    but no matching delete after the JNI up-call, i.e.:

    jenv->CallStaticVoidMethod(..., ..., swigjobj, jmessage);
    jenv->DeleteLocalRef(jmessage);

    Which after enough calls will fill the JNI object reference table with stranded strings and cause a crash when the next JNI object needs to be allocated.

    EDIT:

    An (arguably) better/more flexible solution than the above code that explicitly looks for jstrings to free would be to provide a typemap for directorin cleanup similar to the freearg typemap that could be defined like:

    %typemap(directorinfreearg) string
    %{ jenv->DeleteLocalRef(jenv, $input); %}

     
    Last edit: Will Stampley 2014-05-22

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks