Re: memory leak in SWIG_AsPtr_std_string?
Will Nolan <will <at> headlandstech.com>
2010-05-04 00:33:21 GMT
William Fulton wrote:
> Your analysis looks correct except the leak is probably only in R. I
> think you should base the solution on one of the other UTL language's
> implementation of SWIG_AsCharPtrAndSize, eg in perlstrings.swg and
> pystrings.swg.
>
> Try the attached patch and if it solves the problem, I'll commit it.
Looks like the patch alone doesn't do the trick, although I think your changes are correct (and a
better solution than mine). The code that was generated in my *_wrap.cpp file still had the old pre-patch
code in it
(and thus the leak, verified with valgrind), despite my having applied the patch.
Here's an svn diff that matches the patch contents, just as a sanity check on my part:
[will <at> sfl-dev01 r]$ svn diff rfragments.swg
Index: rfragments.swg
===================================================================
--- rfragments.swg (revision 12008)
+++ rfragments.swg (working copy)
<at> <at> -122,7 +122,7 <at> <at>
SWIG_AsCharPtrAndSize(SEXP obj, char** cptr, size_t* psize, int *alloc)
{
if (cptr && Rf_isString(obj)) {
- const char *cstr = CHAR(STRING_ELT(obj, 0));
+ char *cstr = %const_cast(CHAR(STRING_ELT(obj, 0)), char *);
int len = strlen(cstr);
if (alloc) {
<at> <at> -130,9 +130,7 <at> <at>
*cptr = %new_copy_array(cstr, len + 1, char);
(Continue reading)