Discussion:
[Swig-user] Memory leak sending std::strings to c# callbacks
Paul Knopf
2017-06-11 04:54:27 UTC
Permalink
I originally posted this on GitHub, but there doesn't seem to be much
activity in the issues.

https://github.com/swig/swig/issues/998

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
Paul Knopf
2017-06-11 05:21:01 UTC
Permalink
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 {
Post by Paul Knopf
I originally posted this on GitHub, but there doesn't seem to be much
activity in the issues.
https://github.com/swig/swig/issues/998
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
Loading...