When I embed python in c++,I got memory leak problem.
The following code is a simplified part, when running this, the memory increasing all over the running, which seems the Py_DECREF
didn't work. Is there a way to collect the memory through the circulation so that the memory doesn't go up? Cause when I have a more complicated project the memory consuming is really huge.
#include <Python.h>
float test(double tedata[],int count){
PyObject *datalist=PyList_New(count);
for (int i=0;i<count;i++) {
PyList_SetItem(datalist,i,Py_BuildValue("f",tedata[i]));
}
Py_DECREF(datalist);
printf("for show more slowing");
return 0;
}
void main(){
float pr_label;
double tedata[]= {44.0093,4.4871,305.012201,3.1311,11.1491,109.388698,34.0742,0.04,43.913701};
int count = sizeof(tedata) / sizeof(double);
Py_Initialize();
for(int i=0;i<=500000;i++){
test(tedata,count);
}
Py_Finalize();
}
You are not releasing the objects inside the datalist. Each item in that is having a reference count as well.
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.