Using Valgrind and OpenCV

I have a multithreaded program that uses opencv to read and process images from a usb camera. I have one thread that only reads frames from the camera, and another to process these images. I am running this program on a JetsonNano single board computer, if that is relevant.

It ran as expected until I checked for memory leaks using Valgrind. Under Valgrind, my VideoCapture object is unable to open the camera. I would like to know why this is happening under Valgrind, and not when the program is run normally.

For the purposes of testing, I have removed all of the code related to image processing. The relevant code that remains has been attached below.

//capture definition:

    cv::VideoCapture cap;

//capture initialization:

    CameraController::CameraController : cap(0){}

//frame reading function:

    void CameraController::startReadingFromCamera() {
        while (readingFromCamera) { //boolean member variable
            mutex.lock();

            std::cout << cap.isOpened() << std::endl;
            //this prints 0 under Valgrind, 1 when run without Valgrind 

            //currentFrame is a member variable.  
            //This is how the processing thread will access the newest frame
            cap.read(currentFrame); 
            mutex.unlock();
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
        }
    }

//thread initialization:

    CameraController cameraController;
    thread videoThread(&CameraController::startReadingFromCamera, &cameraController);

I can include the Valgrind output if need be, but I am fairly new to Valgrind. I am not sure which parts of the logs would be useful in analyzing this issue.

Looks like valgrind causes v4l2 driver not able to open the camera.

Are you able to run valgrind with gstreamer pipeline to reproduce this issue?
Also, when you hit this error, do you see any error in dmesg?

Sorry, I have not had a chance to do this yet. I will try it this afternoon.

Do you think my issue could be related to this bug:

https://bugs.kde.org/show_bug.cgi?id=338023

Thanks for the quick response!

Ok I have some updates:

First, running my program under Valgrind yields the following error messages:

(DoVision3:13054): GStreamer-CRITICAL **: 16:31:21.960: gst_mini_object_unref: assertion 'GST_MINI_OBJECT_REFCOUNT_VALUE (mini_object) > 0' failed

(DoVision3:13054): GStreamer-CRITICAL **: 16:31:21.970: gst_value_fixate: assertion 'G_IS_VALUE (src)' failed

Second, I tried running a basic gstreamer program under Valgrind, and it was successful. It opened the camera and showed the video feed as normal. Here is the code I used:

#include <gst/gst.h>

int
main (int argc, char *argv[])
{
  GstElement *pipeline;
  GstBus *bus;
  GstMessage *msg;

  /* Initialize GStreamer */
  gst_init (&argc, &argv);

  /* Build the pipeline */
  pipeline =
      gst_parse_launch
      ("gst-launch-1.0 v4l2src ! xvimagesink",
      NULL);

  /* Start playing */
  gst_element_set_state (pipeline, GST_STATE_PLAYING);

  /* Wait until error or EOS */
  bus = gst_element_get_bus (pipeline);
  msg =
      gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
      GST_MESSAGE_EOS);

  /* Free resources */
  if (msg != NULL)
    gst_message_unref (msg);
  gst_object_unref (bus);
  gst_element_set_state (pipeline, GST_STATE_NULL);
  gst_object_unref (pipeline);
  return 0;
}

Does this mean that the problem lies somewhere in my opencv installation?

I would suggest to try other platform too to see if this is openCV issue.
For example, write a simple application to open usb camera on x86 host and run with valgrind.