title: Gstreamer: what happens with frame buffer memory that is passed into appsink and used in OpenCV Mat?
body: <p>I'm reading videoframe in appsink into opencv Mat:</p>
  <pre><code>GstFlowReturn GStreamPipeline::new_sample(GstElement* sink, GStreamPipeline* gsp) {
      GstSample* sample;
  
      /* Retrieve the buffer */
      g_signal_emit_by_name(sink, &quot;pull-sample&quot;, &amp;sample);
      if (sample) 
      {
  
          GstBuffer* buffer = gst_sample_get_buffer(sample);
          GstSegment* segment = gst_sample_get_segment(sample);
          GstCaps* current_caps = gst_pad_get_current_caps(gst_element_get_static_pad(gsp-&gt;app_sink, &quot;sink&quot;));
          GstStructure* new_pad_struct = gst_caps_get_structure(current_caps, 0);
          gint width = 0, height = 0;
          if (!gst_structure_get_int(new_pad_struct, &quot;width&quot;, &amp;width) ||
              !gst_structure_get_int(new_pad_struct, &quot;height&quot;, &amp;height)) {
              g_print(&quot;No width/height available\n&quot;);
          }
  
          GstMapInfo map;
          gst_buffer_map(buffer, &amp;map, GST_MAP_READ);
  
          cv::Mat frame(cv::Size(width, height), CV_8UC3, (char*)map.data);
          gst_buffer_unmap(buffer, &amp;map);
          if (!current_caps) gst_caps_unref(current_caps);
          gst_sample_unref(sample);
          return GST_FLOW_OK;
      }
  
      return GST_FLOW_ERROR;
  }
  </code></pre>
  <p>What happens with memory to which <code>frame.data</code> points after I leave the scope of the function? How long does that buffer exists? Do I need to copy it to be safe? I want to understand its lifetime to avoid unnecessary copy.</p>