A Warning: OpenCV Memory Leaks

Hopefully this isn’t old news to everyone…
Our code was running fine at SFR but once we got the robot unbagged at SVR it kept crashing every ~30 seconds after code start with an OpenCV memory allocation error. Thanks to the wonderful CSAs here, we figured out that we had a memory leak in our custom camera streaming/ switching code (cscore’s UsbCameras, etc…). If you’re doing anything with Mats for camera streaming or vision, make sure you use


once you’re done with it. You can check your free roboRIO RAM in the second tab of driver station.

Better yet, avoid allocating Mat objects in a loop if at all possible. Pre-allocate and reuse.

As far as know you can’t do that with cscore’s CvSink/Sources… Am I wrong?

You will see in my previous post to the java-2-lifecams thread that it is feasible to resuse a mat. Not certain, perhaps because I avoided ever calling .setSource() ?

Here’s a fragment of our Java code where we create a Mat before entering the loop, then use imageSink.grabFrame to populate the Mat on each iteration:

    CvSink imageSink = new CvSink("CV Image Grabber");

    // All Mats and Lists should be stored outside the loop to avoid allocations
    // as they are expensive to create
    Mat inputImage = new Mat();

    GripPipeline pipe = new GripPipeline();

    // Infinitely process image
    while (true) {
	// Grab a frame. If it has a frame time of 0, there was an error.
	// Just skip and continue
	long frameTime = imageSink.grabFrame(inputImage);

Doh! Thanks, I missed that.