Memory leak sending std::strings to c# callbacks

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Memory leak sending std::strings to c# callbacks

Paul Knopf
I originally posted this on GitHub, but there doesn't seem to be much activity in the issues.


In short, the code being generated by swig that invokes callbacks/delegates in c++, to be passed to c#, is causing memory leaks.

This is the generated code.

NetMethodInfo const *SwigDirector_NetInvokerBase::GetMethodInfo(std::string tt) {
  NetMethodInfo *c_result = 0 ;
  void * jresult = 0 ;
  char * jtt  ;
  
  if (!swig_callbackGetMethodInfo) {
    return NetInvokerBase::GetMethodInfo(tt);
  } else {
    jtt = SWIG_csharp_string_callback((&tt)->c_str());
    jresult = (void *) swig_callbackGetMethodInfo(jtt);
    c_result = (NetMethodInfo *)jresult; 
  }
  return (NetMethodInfo const *)c_result;
}

This is the changed generated code that resolves this problem.

NetMethodInfo const *SwigDirector_NetInvokerBase::GetMethodInfo(std::string tt) {
  NetMethodInfo *c_result = 0 ;
  void * jresult = 0 ;
  char * jtt  ;
  
  if (!swig_callbackGetMethodInfo) {
    return NetInvokerBase::GetMethodInfo(tt);
  } else {
    //jtt = SWIG_csharp_string_callback((&tt)->c_str());
    jresult = (void *) swig_callbackGetMethodInfo((&tt)->c_str());
    c_result = (NetMethodInfo *)jresult; 
  }
  return (NetMethodInfo const *)c_result;
}

Anybody have any idea why this is happening? I am using swig 3.0

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Memory leak sending std::strings to c# callbacks

Paul Knopf
The follow patch fixed the memory leak for std::string. The issue probably still persists with char* and wchar_t*.

diff --git a/Lib/csharp/std_string.i b/Lib/csharp/std_string.i
index 5f8fa44..77194a8 100644
--- a/Lib/csharp/std_string.i
+++ b/Lib/csharp/std_string.i
@@ -42,7 +42,7 @@ class string;
    }
    $result.assign($input); %}

-%typemap(directorin) string %{ $input = SWIG_csharp_string_callback($1.c_str()); %}
+%typemap(directorin) string %{ $input = (char*)$1.c_str(); %}

 %typemap(csin) string "$csinput"
 %typemap(csout, excode=SWIGEXCODE) string {

On Sun, Jun 11, 2017 at 12:54 AM, Paul Knopf <[hidden email]> wrote:
I originally posted this on GitHub, but there doesn't seem to be much activity in the issues.


In short, the code being generated by swig that invokes callbacks/delegates in c++, to be passed to c#, is causing memory leaks.

This is the generated code.

NetMethodInfo const *SwigDirector_NetInvokerBase::GetMethodInfo(std::string tt) {
  NetMethodInfo *c_result = 0 ;
  void * jresult = 0 ;
  char * jtt  ;
  
  if (!swig_callbackGetMethodInfo) {
    return NetInvokerBase::GetMethodInfo(tt);
  } else {
    jtt = SWIG_csharp_string_callback((&tt)->c_str());
    jresult = (void *) swig_callbackGetMethodInfo(jtt);
    c_result = (NetMethodInfo *)jresult; 
  }
  return (NetMethodInfo const *)c_result;
}

This is the changed generated code that resolves this problem.

NetMethodInfo const *SwigDirector_NetInvokerBase::GetMethodInfo(std::string tt) {
  NetMethodInfo *c_result = 0 ;
  void * jresult = 0 ;
  char * jtt  ;
  
  if (!swig_callbackGetMethodInfo) {
    return NetInvokerBase::GetMethodInfo(tt);
  } else {
    //jtt = SWIG_csharp_string_callback((&tt)->c_str());
    jresult = (void *) swig_callbackGetMethodInfo((&tt)->c_str());
    c_result = (NetMethodInfo *)jresult; 
  }
  return (NetMethodInfo const *)c_result;
}

Anybody have any idea why this is happening? I am using swig 3.0


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Loading...