c++算法的python包装器内存泄漏
Memory leaks in python wrapper for C++ algorithm
我正在为c++算法编写一个python包装器。
- 包装器的输入是一个字符串或字符串列表,
- 输出为单个数字或列表。
包装器的主要功能如下:
PyObject* count_rep_list(PyObject *mod, PyObject *args){
PyObject *inputList = PyTuple_GetItem(args, 0);
PyObject *outputList = PyList_New(0);
char* str;
if(PyList_Check(inputList)) {
for (size_t i = 0; i < PyList_Size(inputList); ++i) {
PyObject *list_item = PyList_GetItem(inputList, i);
if (!PyArg_Parse(list_item, "s", &str)) {
Py_XDECREF(list_item);
Py_XDECREF(inputList);
Py_XDECREF(outputList);
return NULL;
}
Py_DECREF(list_item);
PyList_Append(outputList, PyLong_FromSize_t(repeating_count::count(string(str))));
}
}
else if(!PyArg_ParseTuple(args, "s", &str)){
Py_XDECREF(inputList);
Py_XDECREF(outputList);
return NULL;
}
else {
PyList_Append(outputList, PyLong_FromSize_t(repeating_count::count(string(str))));
}
return outputList;
}
repeating_count::count()
的实现无关紧要。
我知道,PyArg_Parse()
和PyArg_ParseTuple()
为str
动态分配内存。但是,如果解析失败,我如何释放这些内存呢?我不知道如何分配这个内存,因此我不能释放它。
-
free(str)
, -
delete(str)
, -
delete str
, -
delete[] str
不工作。
你能帮我吗?
From the docs:
https://docs.python.org/2.0/ext/parseTuple.html不能为字符串本身提供存储空间;指针指向现有字符串存储在字符指针变量中您经过的地址
你得到一个指向python托管字符串的指针,你不负责释放内存
相关文章:
- 从构造函数抛出异常时如何克服内存泄漏
- malloc() 可能出现内存泄漏
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 尽管遵循了规则,内存泄漏在哪里
- 为什么调用堆栈数组会导致内存泄漏
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 使用模板类的自定义列表类型中的内存泄漏
- 为什么以下C++代码中存在内存泄漏?
- OpenCV 我应该使用智能指针来防止内存泄漏吗?
- 我是否生成线程并导致内存泄漏?
- 多线程程序中出现意外的内存泄漏
- 为什么此函数会导致内存泄漏?
- 在 C++ 库中使用cythonized python时内存泄漏
- 需要帮助查找内存泄漏
- 瓦尔格林德的内存泄漏使用新的
- 无法找出我的代码中的内存泄漏
- C++ 结构内部的unordered_map会导致内存泄漏问题吗?
- 可视化 使用 VS Code 查找C++应用程序中的内存泄漏
- Shared_ptr双链接列表内存泄漏
- C++ 在类中使用常量引用文本时 O2 内存泄漏