<< return to Pixycam.com

Pixy 2.1 python free(): invalid next size (fast) error

I’m encountering this error when running a python script on a raspberry pi 5, and it instantly aborts the process without even printing a stacktrace or something. The weirdest thing is it happens at seemingly random intervals, not when is request the ccc blocks. I couldn’t find any examples of others encountering this problem, what could be causing this?
The pixy-relevant portion of the script (basically just detetcting whenever the robot sees a red or a green object):

import pixy
pixy.init()

def getColor(attempts:int=5)->int:
    i=0
    rt=-1
    while rt==-1 and i<attempts:
        blocks=pixy.BlockArray(1)
        n=pixy.ccc_get_blocks(100,blocks)
        log("detected %s blocks"%n,INFO)
        for i in range(n):
            b:pixy.Block=blocks[i]
            log("detected at %s %s, size %s %s with signature: %s"%(b.m_x,b.m_y,b.m_width,b.m_height,b.m_signature),INFO)
        if n>1:
            beep_parallel()
            log("multiple blocks detected!",WARNING)
        if n==0:
            beep_parallel()
            log("no blocks detected!",WARNING)
            rt=-1
        else:            
            b1:pixy.Block=blocks[0]
            rt= GREEN if b1.m_signature==1 else RED
        i+=1
        sleep(0.1)
    if rt==-1:
        log("did not find object!",ERROR)
        rt=GREEN
    log("Detected final color %s"%("GREEN" if rt==GREEN else "RED"),INFO)
    return rt

I tried to provide the right context, but if something else is needed let me know!
Thanks,
Csaba

Hello,
Can you reproduce the abort running the unchanged Pixy Python examples?

Edward

Hi, in the meantime I figured it out, it was an error entirely on my part, but I will share what I did wrong in case somebody makes the same mistake.
The problem is with this line of code:

blocks=pixy.BlockArray(1)

And this line:

pixy.ccc_get_blocks(100,blocks)

So I create a BlockArray with a size of one, but put up to 100 blocks of data in it :sweat_smile:
So this is actually a super simple fix.
P.S.:
While searching what could be causing my problem, I noticed that it only crashes at the fourth time I do this, which still seems a little weird (how doesn’t it crash the first time?).

Hello,
Nice work finding the issue :slight_smile: Python manages memory in an inscrutable way. I’m guessing the overwriting only starts being a problem after several calls to ccc_get_blocks().

Edward