Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将列表作为参数发送时验证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(); } ProgramLiveProcess::ProgramLiveProcess(){ //启动Python以运行程序Python代码 Py_初始化(); //导入程序实时处理模块 program\u live\u processing\u module=PyImport\u ImportModule(模块名称); //获取要调用的函数的对象 live_process=PyObject_GetAttrString(program_live_processing_模块,“program_LiveProcess”); } //从强度数组创建Python列表 PyObject*ProgramLiveProcess::获取强度列表(无符号整数强度[强度数据大小]){ PyObject*tr=PyList_New(0); 对于(int i=0;i传感器\u 1\u温度=(浮动)PyFloat\u为双精度(s0t); //释放python对象并返回 Py_DECREF(s0p); Py_减量(s0t); 返回到_返回; } //释放对象后完成解释器 ProgramLiveProcess::~ProgramLiveProcess(){ Py_DECREF(实时过程); 免费(实时处理); Py_Finalize(); }_Python_C++_Cpython - Fatal编程技术网

将列表作为参数发送时验证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(); } ProgramLiveProcess::ProgramLiveProcess(){ //启动Python以运行程序Python代码 Py_初始化(); //导入程序实时处理模块 program\u live\u processing\u module=PyImport\u ImportModule(模块名称); //获取要调用的函数的对象 live_process=PyObject_GetAttrString(program_live_processing_模块,“program_LiveProcess”); } //从强度数组创建Python列表 PyObject*ProgramLiveProcess::获取强度列表(无符号整数强度[强度数据大小]){ PyObject*tr=PyList_New(0); 对于(int i=0;i传感器\u 1\u温度=(浮动)PyFloat\u为双精度(s0t); //释放python对象并返回 Py_DECREF(s0p); Py_减量(s0t); 返回到_返回; } //释放对象后完成解释器 ProgramLiveProcess::~ProgramLiveProcess(){ Py_DECREF(实时过程); 免费(实时处理); Py_Finalize(); }

将列表作为参数发送时验证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(); } ProgramLiveProcess::ProgramLiveProcess(){ //启动Python以运行程序Python代码 Py_初始化(); //导入程序实时处理模块 program\u live\u processing\u module=PyImport\u ImportModule(模块名称); //获取要调用的函数的对象 live_process=PyObject_GetAttrString(program_live_processing_模块,“program_LiveProcess”); } //从强度数组创建Python列表 PyObject*ProgramLiveProcess::获取强度列表(无符号整数强度[强度数据大小]){ PyObject*tr=PyList_New(0); 对于(int i=0;i传感器\u 1\u温度=(浮动)PyFloat\u为双精度(s0t); //释放python对象并返回 Py_DECREF(s0p); Py_减量(s0t); 返回到_返回; } //释放对象后完成解释器 ProgramLiveProcess::~ProgramLiveProcess(){ Py_DECREF(实时过程); 免费(实时处理); Py_Finalize(); },python,c++,cpython,Python,C++,Cpython,虽然程序可以很好地接收数据,但在不同的条件下运行时,我发现内存泄漏和崩溃方面存在不一致。虽然我不能给出堆栈跟踪,但我想知道我在创建或取消引用Python对象方面是否做错了什么。通常,该程序只在我注释掉free_intensity_list调用时运行 提前感谢。在您的自由强度列表中,您可能正在进行双重自由。考虑使用 PyxXRefff < /代码>。< /P> void Py_DECREF(PyObject *o) Decrement the reference count for object

虽然程序可以很好地接收数据,但在不同的条件下运行时,我发现内存泄漏和崩溃方面存在不一致。虽然我不能给出堆栈跟踪,但我想知道我在创建或取消引用Python对象方面是否做错了什么。通常,该程序只在我注释掉free_intensity_list调用时运行


提前感谢。

在您的
自由强度列表中,您可能正在进行双重自由。考虑使用<代码> PyxXRefff < /代码>。< /P>
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.
资料来源:


此外,您还可以检查双自由。

C标记已删除。请注意,C和C++是不同的语言,不应该集中在一起。为什么你不能给出堆栈跟踪?显示类体,这样我们就可以很容易地看到所有的成员变量。在get_intensities_list()中,列表中的每个条目的ref count都太高,因为在将对象添加到列表后,for循环中的对象没有Py_DECREF。您可能正在尝试进行可能错误的奇怪清理。