简体   繁体   中英

cython double free or corruption

I've coded recently a python function in cython to accelerate it. I don't know C really well, and it appears that I'm forgetting something, since I get an error when I execute my code.

This is my Cython function code writted in histogram.pyx:

# -*- 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

It seems that the function works. I compile the code with this setup.py:

from distutils.core import setup
from Cython.Build import cythonize


setup(ext_modules = cythonize('histogram.pyx'))

And then I try to use it in this script :

from histogram import histogram
import numpy as np
import cv2
from skimage.exposure import rescale_intensity as rsc
import skimage.transform as tf
import pdb


def mutual_information(image1,image2,transfo):
    i =0
    h = histogram(image1,image2,transfo)
    f,r = np.shape(h)
    hf = np.sum(h,axis=1)
    hr = np.sum(h,axis=0)
    n = np.sum(h)
    somme = 0.
    hfr = np.nditer(h,flags=['multi_index'])
    while not hfr.finished:
        if hfr[0] != 0:
            i,j = hfr.multi_index 
            temp= hfr[0]*np.log2((n*hfr[0])/(hf[i]*hr[j]))
            somme+= temp
            #pdb.set_trace()
        hfr.iternext()
    i = somme
    pdb.set_trace()
    return i

img = cv2.imread('frame_18.png',cv2.IMREAD_GRAYSCALE)
im2 = cv2.imread('frame_18.png',cv2.IMREAD_GRAYSCALE)
p10, p90 = np.percentile(img, (10, 90))

img = rsc(img,in_range=(p10,p90))
im2 = rsc(im2,in_range=(p10,p90))
tform = tf.AffineTransform(translation=(+0.,0.),scale = (1.,1.),rotation =0.22,shear =0.0)

def mi(t):
    global img
    global im2
    tform = tf.AffineTransform(translation = (t[0],t[1]),scale=(t[2],t[3]),rotation =t[4],shear = t[5])
    x =-mutual_information(img,im2,tform)
    return x

h = histogram (img,im2,tform)
pdb.set_trace()
print(mi([0.,0.,1.,1.5,0.22,0.]))

There is no problem when I call the function histogram(image1,image2,transfo) and register it in a local variable. But when I use it and then quit (with a return for example), I get the following C error (the script only crashes at the last pdb.set_trace(), and the result of mi function is printed):

*** 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)

After some researches, it seems effectively that it comes from a memory gestion problem for the np.ndarray h, but I couldn't find out what it could be. Does somebody once had the same problem? I hope that I've put enough details so someone will understand the problem.

PS: Sorry for my bad english!

Turning Boundschecking back on worked for me. I had an error of type, I put char but I should be working with unsigned char. With this added, everything is working on!!

Thank you very much for your tips!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM