发送列表作为参数时验证Python参考计数

用户名

我有一个C ++类,它将Python模块作为文件导入,并在返回接收到的数据之前连续为其调用一个函数:

ProgramLiveProcess::ProgramLiveProcess() {
    //Start Python to run the Program Python code
    Py_Initialize();

    //Import the Program live processing module
    program_live_processing_module = PyImport_ImportModule(MODULE_NAME);

    //Get the objects for the functions to call
    live_process = PyObject_GetAttrString(program_live_processing_module, "Program_LiveProcess");
}

//Creates Python list from intensities array
PyObject* ProgramLiveProcess::get_intensities_list(unsigned int intensities[INTENSITIES_DATA_SIZE]) {
    PyObject* tr = PyList_New(0);
    for (int i = 0; i < INTENSITIES_DATA_SIZE; i++) {
        PyObject* ta = PyLong_FromLong(intensities[i]);
        PyList_Append(tr, ta);
    }
    return tr;
}

//TODO: Make this actually work
//Frees objects in intensities Python list and the list itself
void ProgramLiveProcess::free_intensities_list(PyObject* i_list) {
    //std::cout << std::to_string(i_list->ob_refcnt) << std::endl;
    for (int i = 0; i < INTENSITIES_DATA_SIZE; i++) {
        PyObject* ta = PyList_GET_ITEM(i_list, i);
        Py_DECREF(ta);
    }
    Py_DECREF(i_list);
}

processed_data* ProgramLiveProcess::send_to_program_live_process(unsigned int intensities[INTENSITIES_DATA_SIZE], bool use_sensor_1) {
    //Call the Program_LiveProcess function
    PyObject* intensities_py = get_intensities_list(intensities);
    PyObject* calculate_args = PyTuple_Pack(2, intensities_py, use_sensor_1 ? Py_True : Py_False); //True or false depending on if using sensor 1 or 2
    PyObject* processed_data_tuple = PyObject_CallObject(live_process, calculate_args);
    Py_DECREF(calculate_args);
    free_intensities_list(intensities_py);

    //Get the data from the function
    PyObject* s0p = PyTuple_GetItem(processed_data_tuple, 0);
    PyObject* s0t = PyTuple_GetItem(processed_data_tuple, 1);

    //Return a struct containing the data
    processed_data* to_return = (processed_data*)malloc(PROCESSED_DATA_STRUCT_SIZE);
    if (to_return == NULL)
        return to_return;
    to_return->sensor_1_pressure = (float)PyFloat_AS_DOUBLE(s0p);
    to_return->sensor_1_temperature = (float)PyFloat_AS_DOUBLE(s0t);

    //Free python objects and return
    Py_DECREF(s0p);
    Py_DECREF(s0t);
    return to_return;
}

//Finalize the interpreter after freeing objects
ProgramLiveProcess::~ProgramLiveProcess() {
    Py_DECREF(live_process);
    free(live_process);
    Py_Finalize();
}

虽然程序可以正常接收数据,但是在不同条件下运行时,我在内存泄漏和崩溃方面遇到了不一致的情况。虽然我无法给出堆栈跟踪信息,但我想知道在创建或取消引用Python对象方面是否做错了什么。通常,仅当我注释掉free_intensities_list调用时,程序才会运行。

提前致谢。

荷马6

在您的free_intensities_list职能中,您可能正在执行两次释放。考虑使用Py_XDECREF代替。

void Py_DECREF(PyObject *o)
Decrement the reference count for object o. The object must not be NULL; if you aren’t sure that it isn’t NULL, use Py_XDECREF(). If the reference count reaches zero, the object’s type’s deallocation function (which must not be NULL) is invoked.

资料来源:https : //docs.python.org/3/c-api/refcounting.html

此外,您还可以使用MALLOC_CHECK检查双重的FreeS。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

传递列表参数作为参考

来自分类Dev

发送身份验证凭据作为GET参数时的安全问题

来自分类Dev

发送对象列表作为导航参数

来自分类Dev

Mockito:验证通用列表作为参数

来自分类Dev

如何使用ctypes模块通过引用EnumChildWindows将Python列表作为参数发送?

来自分类Dev

Python - 将参数作为结构发送

来自分类Dev

Python参考计数

来自分类Dev

PyGTK自定义信号发送列表作为参数

来自分类Dev

当将空列表作为函数的默认参数时,python为什么会显示此行为?

来自分类Dev

Python ::将列表作为参数传递

来自分类Dev

发送对象作为函数的参数时移动语义

来自分类Dev

方法将函数作为参数时,Mockito验证失败

来自分类Dev

PHP:发送选项列表作为参数(替代命名参数/参数包)

来自分类Dev

如何在python中将结构作为参数发送

来自分类Dev

将Python函数作为Boost.Function参数发送

来自分类Dev

将Python函数作为Boost.Function参数发送

来自分类Dev

在递归函数中传递大量列表作为参数时的性能?

来自分类Dev

关于参考的python列表

来自分类Dev

Python参考附加列表

来自分类Dev

Python参考附加列表

来自分类Dev

Java:修改作为参考传递的参数

来自分类Dev

字符串作为参考参数

来自分类Dev

在作为参考传递的参数中创建对象

来自分类Dev

将键传递给函数作为字符串参数时,VBA Excel错误“按参考参数类型不匹配”

来自分类Dev

将键传递给函数作为字符串参数时,VBA Excel错误“按参考参数类型不匹配”

来自分类Dev

Alamofire发送对象作为参数

来自分类Dev

jQuery发送事件作为参数

来自分类Dev

淘汰发送ViewModel作为参数

来自分类Dev

发送窗口作为命令参数

Related 相关文章

热门标签

归档