代码之家  ›  专栏  ›  技术社区  ›  Michael

C++ Python扩展程序结束后的“损坏的双链表”

  •  0
  • Michael  · 技术社区  · 6 年前

    我正在开发一个用C++代码扩展Python的程序,看起来我在初始化或清理过程中缺少了一些东西。症状是当程序终止时,它会打印出如下内容:

    *** glibc detected *** ... corrupted double-linked list: 0x0000000001ad0700 ***
    ======= Backtrace: =========
    .../python2.7/site-packages/numpy/core/../.libs/libgfortran-ed201abd.so.3.0.0(+0xc249a)[0x7f242063749a]
    ...
    

    我这样做是为了初始化:

    PyMODINIT_FUNC init_dosemap()
    {
        Py_InitModule3( "_dosemap", methods, ... );
        import_array();
    }
    

    数组初始化如下:

    npy_intp dims[] = { width, height };
    int nd = sizeof(dims) / sizeof(dims[0]);
    PyObject * ndArray = PyArray_SimpleNew( nd , dims, NPY_FLOAT32 );
    Py_INCREF( ndArray );
    

    for( int y = 0; y < dims[ 1 ]; ++y )
        for( int x = 0; x < dims[ 0 ]; ++x )
            * (float *) PyArray_GETPTR2( ndArray, x, y ) = ...
    

    然后就这样回来了:

    PyObject* out = Py_BuildValue( "(Offf)", ndArray, ... );
    Py_INCREF( out );
    return out;
    

    0 回复  |  直到 6 年前