简体   繁体   中英

Possible memory leaks

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;
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM