Program crash using the GST buffer and map functions

We use GST functions to create a screenshot from the buffer that we use as a reference for a feature on our app. We find that the code works fine the majority of the time, but will crash the program on this code lines:
gst_buffer_unmap(buf, &map_info);

and

gst_buffer_map(buf, &map_info, GST_MAP_READ)

Essentially what the full code is trying tto accomplish is:

  1. GstSample Retrieval: It retrieves the last sample from a sink element in the GStreamer pipeline. This is done using g_object_get() function.
  2. Extracting Information from the Sample: The function then retrieves information about the sample such as its capabilities (width, height, format) and the buffer containing the image data.
  3. Mapping Buffer and Validating Data: It maps the buffer to a GstMapInfo structure, which provides information about the mapped memory, including the pointer to the data. It checks if the mapping was successful and if the data pointer is valid.
  4. Creating an OpenCV Mat: Using the information retrieved (width, height, data pointer), it creates an OpenCV Mat object to store the image data. The image data is then copied into this Mat.
  5. Color Conversion: It converts the color format of the captured frame from RGB to BGR. This is likely done because OpenCV typically uses BGR color format.
  6. Cleanup and Return: It releases mapped buffer and returns true if the process is successful, otherwise false.

What could be causing these random crashes, and is there a way to do the same thing we are trying to accomplish with a different function?

I think we need to see some actual code.

Perhaps you could share a minimal but complete example?

This also sounds very similar to https://discourse.gstreamer.org/t/gst-buffer-map-and-gst-buffer-unmap-issue . That’s also unresolved but has some code at least.

Code snippet and further context can be found here:

https://discourse.gstreamer.org/t/gst-buffer-map-and-gst-buffer-unmap-issue

That’s not enough to compile and run though, see my reply over there.