2012-03-31 43 views
0

vector during a recursion to push_back`來自某個類的一些實例,但不幸的是我得到了分段錯誤。cython libcpp.vector在遞歸過程中的分段錯誤

這裏的地方出現此錯誤時,以及簡化版本:

PYX文件

from libcpp.vector cimport vector 
from cython.operator cimport dereference as deref, preincrement as inc 


cdef class Grid: 
    cdef unsigned int srow 
    cdef unsigned int erow 
    cdef unsigned int scol 
    cdef unsigned int ecol 

    def __init__(self, unsigned int srow, unsigned int erow, unsigned int scol, 
      unsigned int ecol): 
     self.srow = srow 
     self.erow = erow 
     self.scol = scol 
     self.ecol = ecol 



cdef simple_function_c(vector[Grid] &vp , unsigned int counter): 

    cdef Grid p 

    if counter == 10: 
     return 

    p = Grid(counter-1, counter, counter-1 , counter+1) 
    vp.push_back(p) 
    counter += 1 
    simple_function_c(vp, counter) 

def simple_function(): 

    cdef vector[Grid] vp 
    cdef unsigned int counter 
    cdef Grid tp 

    counter = 0 
    simple_function_c(vp, counter) 

    print vp.size() #this works and outputs 10 

    cdef vector[Grid].iterator it = vp.begin() 
    while it != vp.end(): 
     tp = deref(it) #Seg FAUL !!! 
     print tp.srow, tp.erow, tp.scol, tp.ecol 
     inc(it) 

PY文件

from testrec import simple_function 
simple_function() 

setup.py

from distutils.core import setup 
from distutils.extension import Extension 
from Cython.Distutils import build_ext 

setup(ext_modules=[Extension(
        "testrec", 
        ["testrec.pyx"], 
        language="c++")], 
     cmdclass={'build_ext': build_ext}) 

我沒有爲什麼這麼想s正在發生。我注意到,當類僅有兩個字段我沒有得到一個賽格故障:很奇怪

回答

0

更新:修正只是做

cdef struct Grid: 
    unsigned int srow 
    unsigned int erow 
    unsigned int scol 
    unsigned int ecol