1
我最近编写了一个cython中的python函数来加速它。我不太了解C,看来我忘记了一些东西,因为我在执行代码时遇到错误。cython double free or corruption
这是histogram.pyx创作的我用Cython功能代码:
# -*- coding: utf-8 -*-
import numpy as np
import pdb
import skimage.transform as tf
import cv2
from skimage.exposure import rescale_intensity as rsc
cimport numpy as np
cimport cython
from libc.math cimport floor,ceil
@cython.boundscheck(False)
@cython.wraparound(False)
def histogram(np.ndarray[char, ndim=2] image1,np.ndarray[char, ndim=2] image2, transfo):
cdef int py,px
cdef int max1 = np.amax(image1)
cdef int max2 = np.amax(image2)
cdef int lar = image1.shape[0]
cdef int lon = image1.shape[1]
cdef np.ndarray[np.float64_t, ndim=2] H = np.zeros((max2+1,max1+1),dtype = np.float64)
cdef int larc = image2.shape[0]
cdef int lonc = image1.shape[1]
cdef np.ndarray[np.float64_t, ndim =3] transformed = tf.warp_coords(transfo,(larc,lonc))
cdef double qy,qx,dy,dx,qytemp,qxtemp
cdef int qx0,qy0,x,y,qxi,qyj
cdef double w[4]
cdef char rk,f
cdef int utile = 2
for py in range(larc):
for px in range(lonc):
f = image2[py,px]
qy = transformed[0,py,px]
qx = transformed[1,py,px]
qxtemp = floor(qx)
qytemp = floor(qy)
qx0 =int(qx)
qy0 =int(qy)
dx = qx-qxtemp
dy = qy-qytemp
w[0]=(1-dx)*(1-dy)
w[1]=(1-dx)*dy
w[2]=dx*(1-dy)
w[3]=dx*dy
for x in [0,1]:
for y in [0,1]:
qxi = qx0+x
qyj = qy0+y
if 0<=qxi and qxi<lon and 0<=qyj and qyj<lar:
rk= image1[qyj,qxi]
H[f,rk]+=w[y+2*x]
else:
H[f,0]+= w[y+2*x]
return H
看来,功能工作。我编译代码与此setup.py:
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize('histogram.pyx'))
然后我尝试在这个脚本中使用它:
有没有问题,当我调用函数直方图(图像1,图像2, transfo)并将其注册到局部变量中。但是,当我使用它,然后退出(例如返回)时,我得到以下C错误(该脚本只在最后一个pdb.set_trace()崩溃,并打印出mi功能的结果):
*** glibc detected *** python: double free or corruption (out): 0x0000000002077ff0 ***
======= Backtrace: =========
/lib64/libc.so.6[0x3110475dee]
/lib64/libc.so.6[0x3110478c80]
/cea/home/dev_tse/dev_tse/applications/python/packages/lib/python2.7/site-packages/numpy/core/multiarray.so(+0x1e2bf)[0x2ad8b632f2bf]
/cea/home/dev_tse/dev_tse/applications/python/packages/lib/python2.7/site-packages/numpy/core/multiarray.so(+0x211ce)[0x2ad8b63321ce]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(+0x9b88b)[0x2ad8af1b888b]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(+0x9b88b)[0x2ad8af1b888b]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(+0x65c6a)[0x2ad8af182c6a]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(+0x6288b)[0x2ad8af17f88b]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(+0x852a0)[0x2ad8af1a22a0]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(_PyObject_GenericSetAttrWithDict+0xaa)[0x2ad8af1c06ca]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(PyObject_SetAttr+0x87)[0x2ad8af1c0147]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x1835)[0x2ad8af2245a5]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(PyEval_EvalCodeEx+0x8bc)[0x2ad8af22da3c]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x762a)[0x2ad8af22a39a]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(PyEval_EvalFrameEx+0x784a)[0x2ad8af22a5ba]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(PyEval_EvalCodeEx+0x8bc)[0x2ad8af22da3c]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(PyEval_EvalCode+0x19)[0x2ad8af22db39]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(PyRun_FileExFlags+0x8a)[0x2ad8af251f9a]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(PyRun_SimpleFileExFlags+0xd5)[0x2ad8af253375]
/usr/local/ccc_python/2.7.12_2017.01.2/lib/libpython2.7.so.1.0(Py_Main+0xc61)[0x2ad8af269921]
/lib64/libc.so.6(__libc_start_main+0xfd)[0x311041ed1d]
python[0x400791]
======= Memory map: ========
00400000-00401000 r-xp 00000000 00:15 12798505 /ccc/products1/ccc_python/RedHat-6-x86_64/2.7.12_2017.01.2/bin/python2.7
00600000-00601000 r--p 00000000 00:15 12798505 /ccc/products1/ccc_python/RedHat-6-x86_64/2.7.12_2017.01.2/bin/python2.7
00601000-00602000 rw-p 00001000 00:15 12798505 /ccc/products1/ccc_python/RedHat-6-x86_64/2.7.12_2017.01.2/bin/python2.7
00f88000-0269b000 rw-p 00000000 00:00 0 [heap]
3110000000-3110020000 r-xp 00000000 08:02 405704 /lib64/ld-2.12.so
3110220000-3110221000 r--p 00020000 08:02 405704 /lib64/ld-2.12.so
3110221000-3110222000 rw-p 00021000 08:02 405704 /lib64/ld-2.12.so
3110222000-3110223000 rw-p 00000000 00:00 0
3110400000-311058a000 r-xp 00000000 08:02 406012 /lib64/libc-2.12.so
311058a000-311078a000 ---p 0018a000 08:02 406012 /lib64/libc-2.12.so
311078a000-311078e000 r--p 0018a000 08:02 406012 /lib64/libc-2.12.so
311078e000-3110790000 rw-p 0018e000 08:02 406012 /lib64/libc-2.12.so
3110790000-3110794000 rw-p 00000000 00:00 0
3110800000-3110883000 r-xp 00000000 08:02 406041 /lib64/libm-2.12.so
3110883000-3110a82000 ---p 00083000 08:02 406041 /lib64/libm-2.12.so
3110a82000-3110a83000 r--p 00082000 08:02 406041 /lib64/libm-2.12.so
3110a83000-3110a84000 rw-p 00083000 08:02 406041 /lib64/libm-2.12.so
3110c00000-3110c17000 r-xp 00000000 08:02 406022 /lib64/libpthread-2.12.so
3110c17000-3110e17000 ---p 00017000 08:02 406022 /lib64/libpthread-2.12.so
3110e17000-3110e18000 r--p 00017000 08:02 406022 /lib64/libpthread-2.12.so
3110e18000-3110e19000 rw-p 00018000 08:02 406022 /lib64/libpthread-2.12.so
3110e19000-3110e1d000 rw-p 00000000 00:00 0
3111000000-3111002000 r-xp 00000000 08:02 406039 /lib64/libdl-2.12.so
3111002000-3111202000 ---p 00002000 08:02 406039 /lib64/libdl-2.12.so
3111202000-3111203000 r--p 00002000 08:02 406039 /lib64/libdl-2.12.so
3111203000-3111204000 rw-p 00003000 08:02 406039 /lib64/libdl-2.12.so
3111800000-3111807000 r-xp 00000000 08:02 406116 /lib64/librt-2.12.so
3111807000-3111a06000 ---p 00007000 08:02 406116 /lib64/librt-2.12.so
3111a06000-3111a07000 r--p 00006000 08:02 406116 /lib64/librt-2.12.so
3111a07000-3111a08000 rw-p 00007000 08:02 406116 /lib64/librt-2.12.so
3111c00000-3111c3a000 r-xp 00000000 08:02 406248 /lib64/libreadline.so.6.0
3111c3a000-3111e3a000 ---p 0003a000 08:02 406248 /lib64/libreadline.so.6.0
3111e3a000-3111e42000 rw-p 0003a000 08:02 406248 /lib64/libreadline.so.6.0
3111e42000-3111e43000 rw-p 00000000 00:00 0
3112000000-311201d000 r-xp 00000000 08:02 406143 /lib64/libselinux.so.1
311201d000-311221c000 ---p 0001d000 08:02 406143 /lib64/libselinux.so.1
311221c000-311221d000 r--p 0001c000 08:02 406143 /lib64/libselinux.so.1
311221d000-311221e000 rw-p 0001d000 08:02 406143 /lib64/libselinux.so.1
311221e000-311221f000 rw-p 00000000 00:00 0
3112400000-3112416000 r-xp 00000000 08:02 406071 /lib64/libresolv-2.12.so
3112416000-3112616000 ---p 00016000 08:02 406071 /lib64/libresolv-2.12.so
3112616000-3112617000 r--p 00016000 08:02 406071 /lib64/libresolv-2.12.so
3112617000-3112618000 rw-p 00017000 08:02 406071 /lib64/libresolv-2.12.so
3112618000-311261a000 rw-p 00000000 00:00 0
3118800000-3118803000 r-xp 00000000 08:02 406156 /lib64/libcom_err.so.2.1
3118803000-3118a02000 ---p 00003000 08:02 406156 /lib64/libcom_err.so.2.1
3118a02000-3118a03000 r--p 00002000 08:02 406156 /lib64/libcom_err.so.2.1Abandon (core dumped)
经过一番研究,它似乎很有效地说它来自np.ndarray h的内存问题,但我找不到它可能是什么。有人曾经有过同样的问题吗?我希望我已经提供了足够的细节,以便有人能够理解这个问题。
PS:抱歉我的英文不好!
hi @Dadedidododu,你的脚本很大tbh。你可以试着只给出你的错误代码的摘录,请问? –
可以帮助吗? https://stackoverflow.com/a/37713234/2572645 –
可能不是这样,但很容易尝试重新开始边界检查。 – chrisb