Segfaults in Vision Code (Java)

For vision processing, we have two VisionThreads running, one for each camera, running. The threads pull frames from the camera and push the modified frames to mjpg servers. They also store results as a volitale reference to a VisionResult (an immutable object which stores the co-ordinates of targets, for use by other parts of the code). This part of the code is very simple and I’m confident that it contains no race conditions, but I may be wrong.

Ever since we started (with only one camera), we’ve had random segfaults that crash the whole JVM, or sometimes only crash the single vision thread. They’ve become much more frequent as we started using two cameras. The errors have been a bit varied, and some give incomplete information. Most of them seem to happen in libc. I managed to trace one memory address to somewhere in seekoff for an i/o stream. We’ve also had it that it crashes in free() or other places complaining about corrupted memory. This seems like it indicates some error in cscore, or some other native code.

I’m going to spend the rest of tonight trying to pin-point exactly what parts of our code are necessary for the error, and I’ll post that info here and on an issue in cscore when I figure it out. Has anyone else had this issue?

We found that when we comment out all processing after findContours(), the error still happens, and with findContours() removed, it doesn’t crash (at least not for a while). This still happens even when no mjpeg servers are running at all.

Leaving this here in case anyone had the same issue in the future, and needs such arcane knowledge.

So, after lots of trial-and-error debugging, I’m pretty sure the error came from using findContours(). I doubt that it was an error in how we use findContours, since it was code generated from GRIP that was calling it.

Our solution was to use connectedComponentsWithStats, which filled pretty much the same role.

As a plus, this made us re-write the GRIP pipeline by hand, so it’s a lot more human-readable and re-useable now.