Whenever I call this function, memory usage is increases a lot per call, so I think there is some memory leak here.
PyObject *pScript, *pModule, *pFunc, *pValue;
PyObject *pArgs = NULL;
long ret = 1;
// Initialize python, set system path and load the module
pScript = SetPyObjectString(PYTHON_SCRIPT_NAME);
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('"PYTHON_SCRIPT_PATH"')");
pModule = PyImport_Import(pScript);
Py_XDECREF(pScript);
if (pModule != NULL) {
// Get function object from python module
pFunc = PyObject_GetAttrString(pModule, operation.c_str());
if (pFunc && PyCallable_Check(pFunc)) {
// Create argument(s) as Python tuples
if (operation == UPDATE_KEY) {
// If operation is Update key, create two arguments - key and value
pArgs = PyTuple_New(2);
}
else {
pArgs = PyTuple_New(1);
}
pValue = SetPyObjectString(key.c_str());
// Set argument(s) with key/value strings
PyTuple_SetItem(pArgs, 0, pValue);
if (operation == UPDATE_KEY) {
// If operation is Update key, set two arguments - key and value
pValue = SetPyObjectString(value.c_str());
PyTuple_SetItem(pArgs, 1, pValue);
}
// Call the function using function object and arguments
pValue = PyObject_CallObject(pFunc, pArgs);
Py_XDECREF(pArgs);
if (pValue != NULL) {
// Parse the return values
ret = PyLong_AsLong(PyList_GetItem(pValue, 0));
value = GetPyObjectString(PyList_GetItem(pValue, 1));
}
else {
ERROR("Function call to %s failed", operation.c_str());
}
Py_XDECREF(pValue);
Py_XDECREF(pFunc);
}
else {
ERROR("Cannot find function in python module");
}
Py_XDECREF(pModule);
}
else {
ERROR("Failed to load python module");
}
I am leaking some memory when this C++ snippet in my code calls the python script and I want to know why. I think I am doing something wrong with my Py_DECREFs. Any help would be much appreciated.