robotpy / mostrobotpy

Official Repository of python implementation of WPILib components
https://robotpy.github.io
Other
9 stars 9 forks source link

cscore cv::Mat converter segfault #76

Closed virtuald closed 4 months ago

virtuald commented 5 months ago

Original CD post: https://www.chiefdelphi.com/t/segmentation-fault-from-cvsink/453849/6

The core problem is that the original numpy array is being destroyed, and the new numpy array is referring to the old data. To prevent the crash from happening, you just need to keep a reference to the original data.

Here's a reproducer:

def test_nonempty_img():
    img = np.zeros(shape=(480, 640, 3), dtype=np.uint8)
    sink = cs.CvSink("something")
    _, img = sink.grabFrame(img)
    _, img = sink.grabFrame(img) # crashes here because img is a new numpy object, not the original one