[Cython] memoryview refcount error

Syam Gadde syam.gadde at duke.edu
Tue Jan 28 19:41:35 CET 2014

Hi again,

I'm exploring the typed memoryview feature with numpy and encountered a 
refcount error.  It seems to happen when I create a slice of a 
memoryview, and transpose it.  If I don't do both, I don't get the 
refcount error.  It happens in the error cleanup code.  I suspect that 
any error that causes the function to jump to the error cleanup code 
will do the same thing as the Exception below -- but that seemed the 
easiest way to demonstrate the problem. However, if there truly is an 
error in my code, please let me know! Thanks,


import numpy
cimport numpy

class MyException(Exception):
     def __init__(self):

def testfunc():
    a = numpy.arange(12).reshape([3,4])

    cdef numpy.int_t[:,:] a_view = a

    ## here is a slice followed by a transpose
    cdef numpy.int_t[:,:] b = a_view[:,:].T
    ## same thing happens with a more realistic slicing:
    #cdef numpy.int_t[:,:] b = a_view[1:2,:].T
    ## however, there is no error if I do this instead:
    #cdef numpy.int_t[:,:] b = a_view.T
    ## also no error if I do this instead:
    #cdef numpy.int_t[:,:] b = a_view[:,:]

    # The exception below is just to force the function to abort
    # and run the extraneous __PYX_XDEC_MEMVIEW in the error
    # cleanup code:
    #   Fatal Python error: Acquisition count is 0 (line XXXX)
    # Comment out this exception and we don't get the error
    raise MyException()

except MyException:

More information about the cython-devel mailing list