Menu

#1211 Memory leak after exception

None
closed-fixed
nobody
5
2022-02-17
2011-12-08
Anonymous
No

If a function throws an exception, any applicable 'freearg' typemaps are not executed and leaks may occur.

%typemap(in) Data*
{
$1 = new Data($input)
}

%typemap(freearg) Data*
{
delete $1;
}

class Data
{
// something
};

class Foo
{
public:
void Bar(Data* data) throw (Exception);
};

Discussion

  • Olly Betts

    Olly Betts - 2012-05-18
    • labels: --> code generation (general)
     
  • Olly Betts

    Olly Betts - 2022-02-15
    • status: open --> closed-fixed
    • Group: -->
     
  • Olly Betts

    Olly Betts - 2022-02-15

    Seems to have been fixed since. With SWIG 4.0.2 and Python I get:

    SWIGINTERN PyObject *_wrap_Foo_Bar(PyObject *self, PyObject *args) {
      PyObject *resultobj = 0;
      Foo *arg1 = (Foo *) 0 ;
      Data *arg2 = (Data *) 0 ;
      void *argp1 = 0 ;
      int res1 = 0 ;
      PyObject *swig_obj[2] ;
    
      if (!SWIG_Python_UnpackTuple(args, "Foo_Bar", 2, 2, swig_obj)) SWIG_fail;
      res1 = SWIG_ConvertPtr(swig_obj[0], &argp1,SWIGTYPE_p_Foo, 0 |  0 );
      if (!SWIG_IsOK(res1)) {
        SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Foo_Bar" "', argument " "1"" of type '" "Foo *""'");
      }
      arg1 = reinterpret_cast< Foo * >(argp1);
      {
        arg2 = new Data(swig_obj[1])
      }
      try {
        (arg1)->Bar(arg2);
      } catch(Exception &_e) {
        SWIG_Python_Raise(SWIG_NewPointerObj((new Exception(static_cast< const Exception& >(_e))),SWIGTYPE_p_Exception,SWIG_POINTER_OWN), "Exception", SWIGTYPE_p_Exception); SWIG_fail;
      }
      resultobj = SWIG_Py_Void();
      {
        delete arg2;
      }
      return resultobj;
    fail:
      {
        delete arg2;
      }
      return NULL;
    }
    
     
  • Olly Betts

    Olly Betts - 2022-02-17

    However ... PHP had issues. I've pushed a fix to git master which will be in SWIG 4.1.0.

     

Log in to post a comment.

MongoDB Logo MongoDB