Albert Diserholt
2015-05-06 08:21:41 UTC
Hi,
When wrapping C structure members, SWIG automatically sets the
"wrap:disown" attribute on pointers (cwrap.c:1411). This caused memory
leaks in my script, which wraps a library with constructs such as
these:
struct resp {
struct option1 *opt1;
struct option2 *opt2;
};
Functions in the library take a pointer to a resp struct, and the
caller (my script) is expected to allocate and manage memory for the
optional member pointers (option1 and option2). If my script is not
interested in option2, it sets it to NULL, and the library ignores it.
So, my question is: Why are member pointers automatically disowned? It
sounds counterintuitive to me to have it this way, and it was quite
hard to track down. Simplest way to work around this for me was to use
'sed' on the generated wrap file, which is very ugly. Perhaps a
feature could be introduce to disable the functionality, and not break
backwards compatibility?
Regards,
Albert
When wrapping C structure members, SWIG automatically sets the
"wrap:disown" attribute on pointers (cwrap.c:1411). This caused memory
leaks in my script, which wraps a library with constructs such as
these:
struct resp {
struct option1 *opt1;
struct option2 *opt2;
};
Functions in the library take a pointer to a resp struct, and the
caller (my script) is expected to allocate and manage memory for the
optional member pointers (option1 and option2). If my script is not
interested in option2, it sets it to NULL, and the library ignores it.
So, my question is: Why are member pointers automatically disowned? It
sounds counterintuitive to me to have it this way, and it was quite
hard to track down. Simplest way to work around this for me was to use
'sed' on the generated wrap file, which is very ugly. Perhaps a
feature could be introduce to disable the functionality, and not break
backwards compatibility?
Regards,
Albert