第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

將列表作為參數(shù)發(fā)送時(shí)驗(yàn)證 Python 引用計(jì)數(shù)

將列表作為參數(shù)發(fā)送時(shí)驗(yàn)證 Python 引用計(jì)數(shù)

尚方寶劍之說(shuō) 2023-08-22 14:38:22
我有一個(gè) C++ 類,它將 Python 模塊作為文件導(dǎo)入,并在返回接收到的數(shù)據(jù)之前連續(xù)調(diào)用它的函數(shù):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 arrayPyObject* ProgramLiveProcess::get_intensities_list(unsigned int intensities[INTENSITIES_DATA_SIZE]) {    PyObjec* 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 itselfvoid 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);}雖然程序接收數(shù)據(jù)很好,但在不同條件下運(yùn)行時(shí),我在內(nèi)存泄漏和崩潰方面遇到了不一致的情況。雖然我無(wú)法提供堆棧跟蹤,但我想知道我在創(chuàng)建或取消引用 Python 對(duì)象方面是否做錯(cuò)了什么。通常,程序僅在我注釋掉 free_intensities_list 調(diào)用時(shí)運(yùn)行。
查看完整描述

1 回答

?
動(dòng)漫人物

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超10個(gè)贊

在你的free_intensities_list函數(shù)中,你可能會(huì)進(jìn)行雙重釋放??紤]使用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.
查看完整回答
反對(duì) 回復(fù) 2023-08-22
  • 1 回答
  • 0 關(guān)注
  • 2680 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)