1

Could someone check if there is an memory leak? I am confused.

PyObject * somefunc(int function_id, int num_params, int * params){
    PyObject * params_list=PyList_New(0);
        for(int i=0; i < num_params; i++){
             PyObject * val = Py_BuildValue("i", params[i]);
             PyList_Append(params_list, val);
             Py_DecRef(val);
        }

        PyObject * arglist = Py_BuildValue("(i,O)",
            function_id, params_list);
         //Should I DecRef(params_list) ??

        return arglist;
}
user1618103
  • 51
  • 1
  • 2

1 Answers1

3

As mentioned in the C API documentation, the O format code for Py_BuildValue increments the reference count on its argument, so you are leaking a reference to params_list.

You can fix this by either adding a Py_DECREF call or by using the N format code instead, which acts like O but takes ownership of its argument.

James Henstridge
  • 42,244
  • 6
  • 132
  • 114