gpt4 book ai didi

python - 在已经使用 OpenCV 但版本不同的 C++ 中将 Python 与 OpenCV 嵌入内存损坏

转载 作者:行者123 更新时间:2023-12-04 15:05:08 28 4
gpt4 key购买 nike

我正在尝试将 Python 代码嵌入到 C++ 应用程序中。问题是,如果我在 C++ 代码中使用 OpenCV 函数,也在我嵌入的 python 函数中使用内存损坏。只需注释下面代码中的所有 opencv 函数即可解决问题。一件事是我的 C++ OpenCV 是 4.5.0(动态链接),从源代码编译,而 python 中使用的版本是 3.1.0(使用 python wheel 安装)。

查看回溯(但我不是这方面的专家)在我看来,python 中的调整大小函数在某种程度上依赖于 OpenCV 4.5.0 库来释放内存,导致两个版本之间存在某种冲突。Python 是 3.5.2 版本。

这是一个最小的例子。

我的主要cpp代码:

#include <iostream>
#include <Python.h>
#include <opencv2/opencv.hpp>

using namespace std;


int main()
{

cv::Mat test = cv::imread("/home/rocco/repo/cpp_python/current.jpg");
cv::Mat test_res;
cv::resize(test,test_res,cv::Size(),0.5,0.5);
cv::imwrite("resized.jpg",test_res);
std::string path = "/home/rocco/repo/cpp_python/current.jpg";
std::string filename = "script";
std::string function_name = "myfunction";

std::string wkd = "/home/rocco/repo/cpp_python";

auto script_name = PyUnicode_FromString(filename.c_str());
Py_Initialize();

PyObject *sys_path = PySys_GetObject("path");
PyList_Insert(sys_path, 0, PyUnicode_FromString(wkd.c_str()));
PySys_SetObject("path", sys_path);
PyObject *pModule;
pModule = PyImport_Import(script_name);

auto pFunc = PyObject_GetAttrString(pModule, function_name.c_str());

PyObject * pValue = NULL, * pArgs = NULL;
pArgs = PyTuple_New(2);
auto pFilename = PyUnicode_FromString(path.c_str());

PyTuple_SetItem(pArgs, 0, pFilename);

pValue = PyObject_CallObject(pFunc, pArgs);

Py_DECREF(pArgs);

Py_DECREF(pFunc);
Py_DECREF(pModule);
Py_DECREF(pFilename);


Py_Finalize();
return 0;
}

我的 python 脚本:

import cv2

def myfunction(filename):
img = cv2.imread(filename, cv2.IMREAD_UNCHANGED)
scale_percent = 0.5 # percent of original size
img = cv2.resize(img, None, fx=scale_percent, fy=scale_percent)
cv2.imwrite('resized2.jpg',img)


myfunction('current.jpg')

此处提供回溯:

*** Error in `/home/rocco/repo/cpp_python/cpp_python': free(): invalid next size (fast): 0x0000000002447670 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777f5)[0x7f1a8c9807f5]
/lib/x86_64-linux-gnu/libc.so.6(+0x8038a)[0x7f1a8c98938a]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f1a8c98d58c]
/home/rocco/.local/lib/python3.5/site-packages/cv2/cv2.cpython-35m-x86_64-linux-gnu.so(+0x103b8c)[0x7f1a8639bb8c]
/home/rocco/dynamic_libraries/opencv_4-5/lib/libopencv_core.so.4.5(_ZN2cv3Mat10deallocateEv+0x100)[0x7f1a8d5aa140]
/home/rocco/.local/lib/python3.5/site-packages/cv2/cv2.cpython-35m-x86_64-linux-gnu.so(+0x114fe8)[0x7f1a863acfe8]
/home/rocco/.local/lib/python3.5/site-packages/cv2/cv2.cpython-35m-x86_64-linux-gnu.so(+0x1154d4)[0x7f1a863ad4d4]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyCFunction_Call+0xe9)[0x7f1a90686049]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyEval_EvalFrameEx+0x7555)[0x7f1a90792135]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyEval_EvalFrameEx+0x79d9)[0x7f1a907925b9]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(+0x24ac6c)[0x7f1a90822c6c]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyEval_EvalCodeEx+0x23)[0x7f1a90822d43]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyEval_EvalCode+0x1b)[0x7f1a9078a94b]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(+0x1bf5fd)[0x7f1a907975fd]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyCFunction_Call+0xc9)[0x7f1a90686029]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyEval_EvalFrameEx+0x8c0e)[0x7f1a907937ee]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(+0x24ac6c)[0x7f1a90822c6c]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyEval_EvalFrameEx+0x62d9)[0x7f1a90790eb9]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyEval_EvalFrameEx+0x79d9)[0x7f1a907925b9]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyEval_EvalFrameEx+0x79d9)[0x7f1a907925b9]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyEval_EvalFrameEx+0x79d9)[0x7f1a907925b9]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(+0x24ac6c)[0x7f1a90822c6c]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyEval_EvalCodeEx+0x23)[0x7f1a90822d43]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(+0xd2ac8)[0x7f1a906aaac8]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyObject_Call+0x6e)[0x7f1a9075f46e]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(_PyObject_CallMethodIdObjArgs+0x1bf)[0x7f1a9073d95f]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyImport_ImportModuleLevelObject+0x864)[0x7f1a907c98d4]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(+0x1be638)[0x7f1a90796638]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyCFunction_Call+0xe9)[0x7f1a90686049]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyObject_Call+0x6e)[0x7f1a9075f46e]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyObject_CallFunction+0xcf)[0x7f1a9075f5cf]
/usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0(PyImport_Import+0xe6)[0x7f1a907c9d66]
/home/rocco/repo/cpp_python/cpp_python[0x402291]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f1a8c929840]
/home/rocco/repo/cpp_python/cpp_python[0x401e89]
======= Memory map: ========
00400000-00407000 r-xp 00000000 08:04 9180205 /home/rocco/repo/cpp_python/cpp_python
00606000-00607000 r--p 00006000 08:04 9180205 /home/rocco/repo/cpp_python/cpp_python
00607000-00608000 rw-p 00007000 08:04 9180205 /home/rocco/repo/cpp_python/cpp_python
01e0d000-0251f000 rw-p 00000000 00:00 0 [heap]
7f1a64000000-7f1a64021000 rw-p 00000000 00:00 0
7f1a64021000-7f1a68000000 ---p 00000000 00:00 0
7f1a690f0000-7f1a690f1000 rw-p 00000000 00:00 0
7f1a690f1000-7f1a6a74d000 rw-p 00000000 00:00 0
7f1a6a74d000-7f1a6beeb000 rw-p 00000000 00:00 0
7f1a6beeb000-7f1a6bf7f000 r-xp 00000000 08:04 530555 /home/rocco/.local/lib/python3.5/site-packages/numpy/random/_generator.cpython-35m-x86_64-linux-gnu.so
7f1a6bf7f000-7f1a6c17f000 ---p 00094000 08:04 530555 /home/rocco/.local/lib/python3.5/site-packages/numpy/random/_generator.cpython-35m-x86_64-linux-gnu.so
7f1a6c17f000-7f1a6c1a3000 rw-p 00094000 08:04 530555 /home/rocco/.local/lib/python3.5/site-packages/numpy/random/_generator.cpython-35m-x86_64-linux-gnu.so
7f1a6c1a3000-7f1a6c1a5000 rw-p 00000000 00:00 0
7f1a6c1a5000-7f1a6c1b0000 r-xp 00000000 08:04 530563 /home/rocco/.local/lib/python3.5/site-packages/numpy/random/_sfc64.cpython-35m-x86_64-linux-gnu.so
7f1a6c1b0000-7f1a6c3af000 ---p 0000b000 08:04 530563 /home/rocco/.local/lib/python3.5/site-packages/numpy/random/_sfc64.cpython-35m-x86_64-linux-gnu.so
7f1a6c3af000-7f1a6c3b1000 rw-p 0000a000 08:04 530563 /home/rocco/.local/lib/python3.5/site-packages/numpy/random/_sfc64.cpython-35m-x86_64-linux-gnu.so
7f1a6c3b1000-7f1a6c3bf000 r-xp 00000000 08:04 530561 /home/rocco/.local/lib/python3.5/site-packages/numpy/random/_pcg64.cpython-35m-x86_64-linux-gnu.so
7f1a6c3bf000-7f1a6c5bf000 ---p 0000e000 08:04 530561 /home/rocco/.local/lib/python3.5/site-packages/numpy/random/_pcg64.cpython-35m-x86_64-linux-gnu.so
7f1a6c5bf000-7f1a6c5c1000 rw-p 0000e000 08:04 530561 /home/rocco/.local/lib/python3.5/site-packages/numpy/random/_pcg64.cpython-35m-x86_64-linux-gnu.so
7f1a6c5c1000-7f1a6c5d2000 r-xp 00000000 08:04 530566 /home/rocco/.local/lib/python3.5/site-packages/numpy/random/_philox.cpython-35m-x86_64-linux-gnu.so
7f1a6c5d2000-7f1a6c7d1000 ---p 00011000 08:04 530566 /home/rocco/.local/lib/python3.5/site-packages/numpy/random/_philox.cpython-35m-x86_64-linux-gnu.so
7f1a6c7d1000-7f1a6c7d3000 rw-p 00010000 08:04 530566 /home/rocco/.local/lib/python3.5/site-packages/numpy/random/_philox.cpython-35m-x86_64-linux-gnu.so
7f1a6c7d3000-7f1a6c7d4000 rw-p 00000000 00:00 0
7f1a6c7d4000-7f1a6c7eb000 r-xp 00000000 08:04 530559 /home/rocco/.local/lib/python3.5/site-packages/numpy/random/_mt19937.cpython-35m-x86_64-linux-gnu.so
7f1a6c7eb000-7f1a6c9eb000 ---p 00017000 08:04 530559 /home/rocco/.local/lib/python3.5/site-packages/numpy/random/_mt19937.cpython-35m-x86_64-linux-gnu.so
7f1a6c9eb000-7f1a6c9ed000 rw-p 00017000 08:04 530559 /home/rocco/.local/lib/python3.5/site-packages/numpy/random/_mt19937.cpython-35m-x86_64-linux-gnu.so
7f1a6c9ed000-7f1a6ca3b000 r-xp 00000000 08:04 530556 /home/rocco/.local/lib/python3.5/site-packages/numpy/random/_bounded_integers.cpython-35m-x86_64-linux-gnu.so
7f1a6ca3b000-7f1a6cc3b000 ---p 0004e000 08:04 530556 /home/rocco/.local/lib/python3.5/site-packages/numpy/random/_bounded_integers.cpython-35m-x86_64-linux-gnu.so
7f1a6cc3b000-7f1a6cc3d000 rw-p 0004e000 08:04 530556 /home/rocco/.local/lib/python3.5/site-packages/numpy/random/_bounded_integers.cpython-35m-x86_64-linux-gnu.so
7f1a6cc3d000-7f1a6ce58000 r-xp 00000000 08:04 9437776 /lib/x86_64-linux-gnu/libcrypto.so.1.0.0
7f1a6ce58000-7f1a6d057000 ---p 0021b000 08:04 9437776 /lib/x86_64-linux-gnu/libcrypto.so.1.0.0
7f1a6d057000-7f1a6d073000 r--p 0021a000 08:04 9437776 /lib/x86_64-linux-gnu/libcrypto.so.1.0.0
7f1a6d073000-7f1a6d07f000 rw-p 00236000 08:04 9437776 /lib/x86_64-linux-gnu/libcrypto.so.1.0.0
7f1a6d07f000-7f1a6d082000 rw-p 00000000 00:00 0
7f1a6d082000-7f1a6d088000 r-xp 00000000 08:04 7086087 /usr/lib/python3.5/lib-dynload/_hashlib.cpython-35m-x86_64-linux-gnu.so
7f1a6d088000-7f1a6d287000 ---p 00006000 08:04 7086087 /usr/lib/python3.5/lib-dynload/_hashlib.cpython-35m-x86_64-linux-gnu.so
7f1a6d287000-7f1a6d288000 r--p 00005000 08:04 7086087 /usr/lib/python3.5/lib-dynload/_hashlib.cpython-35m-x86_64-linux-gnu.so
7f1a6d288000-7f1a6d289000 rw-p 00006000 08:04 7086087 /usr/lib/python3.5/lib-dynload/_hashlib.cpython-35m-x86_64-linux-gnu.so
7f1a6d289000-7f1a6d2b9000 r-xp 00000000 08:04 530568 /home/rocco/.local/lib/python3.5/site-packages/numpy/random/_common.cpython-35m-x86_64-linux-gnu.so
7f1a6d2b9000-7f1a6d4b8000 ---p 00030000 08:04 530568 /home/rocco/.local/lib/python3.5/site-packages/numpy/random/_common.cpython-35m-x86_64-linux-gnu.so
7f1a6d4b8000-7f1a6d4ba000 rw-p 0002f000 08:04 530568 /home/rocco/.local/lib/python3.5/site-packages/numpy/random/_common.cpython-35m-x86_64-linux-gnu.so
7f1a6d4ba000-7f1a6d4bb000 rw-p 00000000 00:00 0
7f1a6d4bb000-7f1a6d4e0000 r-xp 00000000 08:04 530557 /home/rocco/.local/lib/python3.5/site-packages/numpy/random/_bit_generator.cpython-35m-x86_64-linux-gnu.so
7f1a6d4e0000-7f1a6d6e0000 ---p 00025000 08:04 530557 /home/rocco/.local/lib/python3.5/site-packages/numpy/random/_bit_generator.cpython-35m-x86_64-linux-gnu.so
7f1a6d6e0000-7f1a6d6e5000 rw-p 00025000 08:04 530557 /home/rocco/.local/lib/python3.5/site-packages/numpy/random/_bit_generator.cpython-35m-x86_64-linux-gnu.so
7f1a6d6e5000-7f1a6d6e6000 rw-p 00000000 00:00 0
7f1a6d6e6000-7f1a6d75e000 r-xp 00000000 08:04 530558 /home/rocco/.local/lib/python3.5/site-packages/numpy/random/mtrand.cpython-35m-x86_64-linux-gnu.so
7f1a6d75e000-7f1a6d95d000 ---p 00078000 08:04 530558 /home/rocco/.local/lib/python3.5/site-packages/numpy/random/mtrand.cpython-35m-x86_64-linux-gnu.so
7f1a6d95d000-7f1a6d983000 rw-p 00077000 08:04 530558 /home/rocco/.local/lib/python3.5/site-packages/numpy/random/mtrand.cpython-35m-x86_64-linux-gnu.so
7f1a6d983000-7f1a6d985000 rw-p 00000000 00:00 0
7f1a6d985000-7f1a6d99b000 r-xp 00000000 08:04 529217 /home/rocco/.local/lib/python3.5/site-packages/numpy/fft/_pocketfft_internal.cpython-35m-x86_64-linux-gnu.so
7f1a6d99b000-7f1a6db9a000 ---p 00016000 08:04 529217 /home/rocco/.local/lib/python3.5/site-packages/numpy/fft/_pocketfft_internal.cpython-35m-x86_64-linux-gnu.so
7f1a6db9a000-7f1a6db9b000 rw-p 00015000 08:04 529217 /home/rocco/.local/lib/python3.5/site-packages/numpy/fft/_pocketfft_internal.cpython-35m-x86_64-linux-gnu.so
7f1a6db9b000-7f1a6dbd2000 r-xp 00000000 08:04 6824744 /usr/lib/x86_64-linux-gnu/libmpdec.so.2.4.2
7f1a6dbd2000-7f1a6ddd1000 ---p 00037000 08:04 6824744 /usr/lib/x86_64-linux-gnu/libmpdec.so.2.4.2
7f1a6ddd1000-7f1a6ddd2000 r--p 00036000 08:04 6824744 /usr/lib/x86_64-linux-gnu/libmpdec.so.2.4.2
7f1a6ddd2000-7f1a6ddd3000 rw-p 00037000 08:04 6824744 /usr/lib/x86_64-linux-gnu/libmpdec.so.2.4.2
7f1a6ddd3000-7f1a6ddf7000 r-xp 00000000 08:04 7150229 /usr/lib/python3.5/lib-dynload/_decimal.cpython-35m-x86_64-linux-gnu.so
7f1a6ddf7000-7f1a6dff6000 ---p 00024000 08:04 7150229 /usr/lib/python3.5/lib-dynload/_decimal.cpython-35m-x86_64-linux-gnu.so
7f1a6dff6000-7f1a6dff7000 r--p 00023000 08:04 7150229 /usr/lib/python3.5/lib-dynload/_decimal.cpython-35m-x86_64-linux-gnu.so
7f1a6dff7000-7f1a6e000000 rw-p 00024000 08:04 7150229 /usr/lib/python3.5/lib-dynload/_decimal.cpython-35m-x86_64-linux-gnu.so
7f1a6e000000-7f1a70000000 rw-p 00000000 00:00 0
7f1a70000000-7f1a70021000 rw-p 00000000 00:00 0
7f1a70021000-7f1a74000000 ---p 00000000 00:00 0
7f1a74000000-7f1a78000000 rw-p 00000000 00:00 0
7f1a78000000-7f1a78021000 rw-p 00000000 00:00 0
7f1a78021000-7f1a7c000000 ---p 00000000 00:00 0
7f1a7c010000-7f1a7c190000 rw-p 00000000 00:00 0
7f1a7c190000-7f1a7c197000 r-xp 00000000 08:04 7136401 /usr/lib/python3.5/lib-dynload/_lzma.cpython-35m-x86_64-linux-gnu.so
7f1a7c197000-7f1a7c396000 ---p 00007000 08:04 7136401 /usr/lib/python3.5/lib-dynload/_lzma.cpython-35m-x86_64-linux-gnu.so
7f1a7c396000-7f1a7c397000 r--p 00006000 08:04 7136401 /usr/lib/python3.5/lib-dynload/_lzma.cpython-35m-x86_64-linux-gnu.so
7f1a7c397000-7f1a7c399000 rw-p 00007000 08:04 7136401 /usr/lib/python3.5/lib-dynload/_lzma.cpython-35m-x86_64-linux-gnu.so
7f1a7c399000-7f1a7c3a8000 r-xp 00000000 08:04 9437491 /lib/x86_64-linux-gnu/libbz2.so.1.0.4
7f1a7c3a8000-7f1a7c5a7000 ---p 0000f000 08:04 9437491 /lib/x86_64-linux-gnu/libbz2.so.1.0.4
7f1a7c5a7000-7f1a7c5a8000 r--p 0000e000 08:04 9437491 /lib/x86_64-linux-gnu/libbz2.so.1.0.4
7f1a7c5a8000-7f1a7c5a9000 rw-p 0000f000 08:04 9437491 /lib/x86_64-linux-gnu/libbz2.so.1.0.4
7f1a7c5a9000-7f1a7c5ad000 r-xp 00000000 08:04 7136410 /usr/lib/python3.5/lib-dynload/_bz2.cpython-35m-x86_64-linux-gnu.so
7f1a7c5ad000-7f1a7c7ac000 ---p 00004000 08:04 7136410 /usr/lib/python3.5/lib-dynload/_bz2.cpython-35m-x86_64-linux-gnu.so
7f1a7c7ac000-7f1a7c7ad000 r--p 00003000 08:04 7136410 /usr/lib/python3.5/lib-dynload/_bz2.cpython-35m-x86_64-linux-gnu.so
7f1a7c7ad000-7f1a7c7ae000 rw-p 00004000 08:04 7136410 /usr/lib/python3.5/lib-dynload/_bz2.cpython-35m-x86_64-linux-gnu.so
7f1a7c7ae000-7f1a7c8ae000 rw-p 00000000 00:00 0
7f1a7c8ae000-7f1a7c8d9000 r-xp 00000000 08:04 530149 /home/rocco/.local/lib/python3.5/site-packages/numpy/linalg/_umath_linalg.cpython-35m-x86_64-linux-gnu.so
7f1a7c8d9000-7f1a7cad9000 ---p 0002b000 08:04 530149 /home/rocco/.local/lib/python3.5/site-packages/numpy/linalg/_umath_linalg.cpython-35m-x86_64-linux-gnu.so
7f1a7cad9000-7f1a7cadb000 rw-p 0002b000 08:04 530149 /home/rocco/.local/lib/python3.5/site-packages/numpy/linalg/_umath_linalg.cpython-35m-x86_64-linux-gnu.so
7f1a7cadb000-7f1a7cade000 rw-p 000d4000 08:04 530149 /home/rocco/.local/lib/python3.5/site-packages/numpy/linalg/_umath_linalg.cpython-35m-x86_64-linux-gnu.so
7f1a7cade000-7f1a7cae2000 r-xp 00000000 08:04 530150 /home/rocco/.local/lib/python3.5/site-packages/numpy/linalg/lapack_lite.cpython-35m-x86_64-linux-gnu.so
7f1a7cae2000-7f1a7cce2000 ---p 00004000 08:04 530150 /home/rocco/.local/lib/python3.5/site-packages/numpy/linalg/lapack_lite.cpython-35m-x86_64-linux-gnu.so
7f1a7cce2000-7f1a7cce3000 rw-p 00004000 08:04 530150 /home/rocco/.local/lib/python3.5/site-packages/numpy/linalg/lapack_lite.cpython-35m-x86_64-linux-gnu.so
7f1a7cce3000-7f1a7cce5000 rw-p 00019000 08:04 530150 /home/rocco/.local/lib/python3.5/site-packages/numpy/linalg/lapack_lite.cpython-35m-x86_64-linux-gnu.so
7f1a7cce5000-7f1a7cda5000 rw-p 00000000 00:00 0
7f1a7cda5000-7f1a7cdc7000 r-xp 00000000 08:04 7136407 /usr/lib/python3.5/lib-dynload/_ctypes.cpython-35m-x86_64-linux-gnu.so
7f1a7cdc7000-7f1a7cfc6000 ---p 00022000 08:04 7136407 /usr/lib/python3.5/lib-dynload/_ctypes.cpython-35m-x86_64-linux-gnu.so
7f1a7cfc6000-7f1a7cfc7000 r--p 00021000 08:04 7136407 /usr/lib/python3.5/lib-dynload/_ctypes.cpython-35m-x86_64-linux-gnu.so
7f1a7cfc7000-7f1a7cfcb000 rw-p 00022000 08:04 7136407 /usr/lib/python3.5/lib-dynload/_ctypes.cpython-35m-x86_64-linux-gnu.so
7f1a7cfcb000-7f1a7cfcc000 rw-p 00000000 00:00 0
7f1a7cfcc000-7f1a7cfeb000 r-xp 00000000 08:04 530237 /home/rocco/.local/lib/python3.5/site-packages/numpy/core/_multiarray_tests.cpython-35m-x86_64-linux-gnu.so
7f1a7cfeb000-7f1a7d1eb000 ---p 0001f000 08:04 530237 /home/rocco/.local/lib/python3.5/site-packages/numpy/core/_multiarray_tests.cpython-35m-x86_64-linux-gnu.so
7f1a7d1eb000-7f1a7d1ed000 rw-p 0001f000 08:04 530237 /home/rocco/.local/lib/python3.5/site-packages/numpy/core/_multiarray_tests.cpython-35m-x86_64-linux-gnu.so
7f1a7d1ed000-7f1a7d26d000 rw-p 00000000 00:00 0
7f1a7d26d000-7f1a7d26e000 ---p 00000000 00:00 0
7f1a7d26e000-7f1a7da6e000 rw-p 00000000 00:00 0
7f1a7da6e000-7f1a7f564000 r-xp 00000000 08:04 532531 /home/rocco/.local/lib/python3.5/site-packages/numpy.libs/libopenblasp-r0-34a18dc3.3.7.so
7f1a7f564000-7f1a7f763000 ---p 01af6000 08:04 532531 /home/rocco/.local/lib/python3.5/site-packages/numpy.libs/libopenblasp-r0-34a18dc3.3.7.so
7f1a7f763000-7f1a7f77c000 rw-p 01af5000 08:04 532531 /home/rocco/.local/lib/python3.5/site-packages/numpy.libs/libopenblasp-r0-34a18dc3.3.7.so
7f1a7f77c000-7f1a7f787000 rw-p 00000000 00:00 0
7f1a7f787000-7f1a7f7ff000 rw-p 01be1000 08:04 532531 /home/rocco/.local/lib/python3.5/site-packages/numpy.libs/libopenblasp-r0-34a18dc3.3.7.so
7f1a7f7ff000-7f1a7f800000 ---p 00000000 00:00 0
7f1a7f800000-7f1a80000000 rw-p 00000000 00:00 0
7f1a80000000-7f1a80021000 rw-p 00000000 00:00 0
7f1a80021000-7f1a84000000 ---p 00000000 00:00 0
7f1a8401d000-7f1a840dd000 rw-p 00000000 00:00 0
7f1a840fe000-7f1a841be000 rw-p 00000000 00:00 0
7f1a841be000-7f1a841bf000 ---p 00000000 00:00 0
7f1a841bf000-7f1a849bf000 rw-p 00000000 00:00 0
7f1a849bf000-7f1a849c0000 ---p 00000000 00:00 0
7f1a849c0000-7f1a851c0000 rw-p 00000000 00:00 0
7f1a851c0000-7f1a852b0000 r-xp 00000000 08:04 532291 /home/rocco/.local/lib/python3.5/site-packages/numpy.libs/libgfortran-ed201abd.so.3.0.0
7f1a852b0000-7f1a854af000 ---p 000f0000 08:04 532291 /home/rocco/.local/lib/python3.5/site-packages/numpy.libs/libgfortran-ed201abd.so.3.0.0
7f1a854af000-7f1a854b1000 rw-p 000ef000 08:04 532291 /home/rocco/.local/lib/python3.5/site-packages/numpy.libs/libgfortran-ed201abd.so.3.0.0
7f1a854b1000-7f1a854b2000 rw-p 00000000 00:00 0
7f1a854b2000-7f1a854ba000 rw-p 000f2000 08:04 532291 /home/rocco/.local/lib/python3.5/site-packages/numpy.libs/libgfortran-ed201abd.so.3.0.0
7f1a854ba000-7f1a8585f000 r-xp 00000000 08:04 530243 /home/rocco/.local/lib/python3.5/site-packages/numpy/core/_multiarray_umath.cpython-35m-x86_64-linux-gnu.so
7f1a8585f000-7f1a85a5e000 ---p 003a5000 08:04 530243 /home/rocco/.local/lib/python3.5/site-packages/numpy/core/_multiarray_umath.cpython-35m-x86_64-linux-gnu.so
7f1a85a5e000-7f1a85a7d000 rw-p 003a4000 08:04 530243 /home/rocco/.local/lib/python3.5/site-packages/numpy/core/_multiarray_umath.cpython-35m-x86_64-linux-gnu.so
7f1a85a7d000-7f1a85a9e000 rw-p 00000000 00:00 0
7f1a85a9e000-7f1a85aa5000 rw-p 01474000 08:04 530243 /home/rocco/.local/lib/python3.5/site-packages/numpy/core/_multiarray_umath.cpython-35m-x86_64-linux-gnu.so
7f1a85aa5000-7f1a85ae5000 rw-p 00000000 00:00 0
7f1a85ae5000-7f1a85b53000 r-xp 00000000 08:04 9441868 /lib/x86_64-linux-gnu/libpcre.so.3.13.2
7f1a85b53000-7f1a85d53000 ---p 0006e000 08:04 9441868 /lib/x86_64-linux-gnu/libpcre.so.3.13.2
7f1a85d53000-7f1a85d54000 r--p 0006e000 08:04 9441868 /lib/x86_64-linux-gnu/libpcre.so.3.13.2
7f1a85d54000-7f1a85d55000 rw-p 0006f000 08:04 9441868 /lib/x86_64-linux-gnu/libpcre.so.3.13.2
7f1a85d55000-7f1a85e64000 r-xp 00000000 08:04 9437261 /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.2
7f1a85e64000-7f1a86063000 ---p 0010f000 08:04 9437261 /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.2
7f1a86063000-7f1a86064000 r--p 0010e000 08:04 9437261 /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.2
7f1a86064000-7f1a86065000 rw-p 0010f000 08:04 9437261 /lib/x86_64-linux-gnu/libglib-2.0.so.0.4800.2
7f1a86065000-7f1a86066000 rw-p 00000000 00:00 0
7f1a86066000-7f1a86067000 r-xp 00000000 08:04 6819030 /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0.4800.2
7f1a86067000-7f1a86266000 ---p 00001000 08:04 6819030 /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0.4800.2
7f1a86266000-7f1a86267000 r--p 00000000 08:04 6819030 /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0.4800.2
7f1a86267000-7f1a86268000 rw-p 00001000 08:04 6819030 /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0.4800.2
7f1a86298000-7f1a870f2000 r-xp 00000000 08:04 528776 /home/rocco/.local/lib/python3.5/site-packages/cv2/cv2.cpython-35m-x86_64-linux-gnu.so
7f1a870f2000-7f1a872f1000 ---p 00e5a000 08:04 528776 /home/rocco/.local/lib/python3.5/site-packages/cv2/cv2.cpython-35m-x86_64-linux-gnu.so
7f1a872f1000-7f1a8733f000 rw-p 00e59000 08:04 528776 /home/rocco/.local/lib/python3.5/site-packages/cv2/cv2.cpython-35m-x86_64-linux-gnu.so
7f1a8733f000-7f1a87465000 rw-p 00000000 00:00 0
7f1a87465000-7f1a8773f000 r--p 00000000 08:04 6816127 /usr/lib/locale/locale-archive
7f1a8773f000-7f1a87740000 ---p 00000000 00:00 0
7f1a87740000-7f1a87f40000 rw-p 00000000 00:00 0
7f1a87f40000-7f1a87f41000 ---p 00000000 00:00 0
7f1a87f41000-7f1a8a336000 rw-p 00000000 00:00 0
7f1a8a336000-7f1a8a33b000 r-xp 00000000 08:04 6823807 /usr/lib/x86_64-linux-gnu/libIlmThread-2_2.so.12.0.0
7f1a8a33b000-7f1a8a53b000 ---p 00005000 08:04 6823807 /usr/lib/x86_64-linux-gnu/libIlmThread-2_2.so.12.0.0

编辑

经过一些测试,我尝试将 python 更新到版本 3.6.13,但问题仍然存在,此外我意识到问题与 cv2.resize 函数无关,而是与一般的 opencv 相关,无论我从 python 调用什么 opencv 函数,它调用我的动态链接 opencv 4.5 库进行释放,导致问题。

编辑

在 C++ 中使用静态链接的 OpenCV,一切都按预期工作。这有什么原因吗?

最佳答案

这似乎是一个链接问题。

Python 使用“dlopen”动态加载库,尝试更改 flags传递给这个函数。例如,使用 RTLD_DEEPBIND,您可以指定首选加载对象包含的符号(而不是具有相同名称的全局符号)。

使用这两行:

import os
print(os.RTLD_DEEPBIND | os.RTLD_NOW)

您可以获得这些标志的int代码,即10。

然后您可以通过调用 sys.setdlopenflags(10) 更改 dlopen 使用的标志:

void setFlags()
{
auto flag = PyLong_FromLong(10);
auto setdl = PySys_GetObject("setdlopenflags");

auto arg = PyTuple_New(1);
PyTuple_SetItem(arg, 0, flag);
PyObject_CallObject(setdl, arg);
}

它必须在 PyImport_Import 之前使用。

关于python - 在已经使用 OpenCV 但版本不同的 C++ 中将 Python 与 OpenCV 嵌入内存损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66297277/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com