Memory usage increasing when using gstreamer + opencv for image capture

Memory_leak.zip (984 Bytes)

Hello. I had a problem using gstreamer, opencv on jetson nano
My configuration is Jetpack 4.4.1 , python 3.6.9 (Default on jetpack), using CSI Camera.
I want to capture of images for a while.
I uploaded my python file. You can reproduce it.
Starting the program, the memory usage is gradually increasing , ran into below problem and stop!
How can i solve it?

NvRmChannelSubmit: NvError_IoctlFailed with error code 22
NvRmPrivFlush: NvRmChannelSubmit failed (err = 196623, SyncPointIdx = 7, SyncPointValue = 0)
fence_set_name ioctl failed with 22
(Argus) Error InsufficientMemory: (propagating from src/eglstream/ImageImpl.cpp, function copyToNvBuffer(), line 513)
Error generated. /dvs/git/dirty/git-master_linux/multimedia/nvgstreamer/gst-nvarguscamera/gstnvarguscamerasrc.cpp, threadExecute:498 IImageNativeBuffer not supported by Image.
Error generated. /dvs/git/dirty/git-master_linux/multimedia/nvgstreamer/gst-nvarguscamera/gstnvarguscamerasrc.cpp, threadFunction:194 (propagating)

Hi,
Do you observe the issue if you run video preview instead of JPEG encoding? A sample is in
OpenCV Video Capture with GStreamer doesn't work on ROS-melodic - #3 by DaneLLL
Would like to know if it works in video preview case.

No.
In your sample code case, I don’t see the memory increasing.
So the problem might be that when call ‘cv2.VideoCapture’ in for loop. It seize the memory and do not free. It cause the problem.
please fix it.

Hi,
The source code of nvarguscamerasrc is open source in
https://developer.nvidia.com/embedded/linux-tegra
L4T Driver Package (BSP) Sources

Please apply the patch, rebuild/replace libgstnvarguscamerasrc.so and try again:

@@ -519,6 +519,8 @@ bool StreamConsumer::threadExecute(GstNvArgusCameraSrc *src)
     g_mutex_unlock (&src->argus_buffer_consumed_lock);
   }
 
+  if (src->frameInfo->fd)
+    NvBufferDestroy(src->frameInfo->fd);
   g_slice_free (NvArgusFrameInfo, src->frameInfo);
   if (!src->argus_in_error)
   {
@@ -1220,6 +1222,10 @@ static gboolean gst_nv_argus_camera_set_caps (GstBaseSrc *base, GstCaps *caps)
       gst_caps_unref (old);
   }
 
+  if (src->pool) {
+    gst_object_unref (src->pool);
+    src->pool = NULL;
+  }
   if (src->bufApi == FALSE){
     src->pool = gst_buffer_pool_new ();
     GstNVArgusMemoryAllocator *allocator =
@@ -1235,6 +1241,7 @@ static gboolean gst_nv_argus_camera_set_caps (GstBaseSrc *base, GstCaps *caps)
     src->nvmm_buffers = g_queue_new ();
 
     gst_buffer_pool_set_active (src->pool, TRUE);
+    gst_object_unref (allocator);
   }
   else {
     src->pool = gst_nvds_buffer_pool_new();
@@ -1292,6 +1299,7 @@ static gboolean gst_nv_argus_camera_unlock_stop (GstBaseSrc *src)
 static gboolean gst_nv_argus_camera_stop (GstBaseSrc * src_base)
 {
   GstNvArgusCameraSrc *src = (GstNvArgusCameraSrc *) src_base;
+  GstBuffer *buf;
   src->stop_requested = TRUE;
   if(!src->timeout)
   {
@@ -1306,8 +1314,21 @@ static gboolean gst_nv_argus_camera_stop (GstBaseSrc * src_base)
   g_mutex_lock (&src->eos_lock);
   g_cond_signal (&src->eos_cond);
   g_mutex_unlock (&src->eos_lock);
-  gst_buffer_pool_set_active (src->pool, false);
   g_thread_join(src->argus_thread);
+  g_thread_join(src->consumer_thread);
+  while (!g_queue_is_empty (src->nvmm_buffers)) {
+    buf = (GstBuffer *) g_queue_pop_head (src->nvmm_buffers);
+    gst_buffer_unref (buf);
+  }
+  if (src->pool) {
+    if (gst_buffer_pool_is_active (src->pool))
+      gst_buffer_pool_set_active (src->pool, false);
+
+    gst_object_unref (src->pool);
+    src->pool = NULL;
+  }
+  g_queue_free(src->argus_buffers);
+  g_queue_free(src->nvmm_buffers);
   return TRUE;
 }
 
@@ -1361,6 +1382,11 @@ consumer_thread (gpointer src_base)
     while (g_queue_is_empty (src->argus_buffers))
     {
       g_cond_wait (&src->argus_buffers_queue_cond, &src->argus_buffers_queue_lock);
+      if (src->stop_requested)
+      {
+        g_mutex_unlock (&src->argus_buffers_queue_lock);
+        goto done;
+      }
     }
 
     consumerFrameInfo = (NvArgusFrameInfo *) g_queue_pop_head (src->argus_buffers);

Hi.
Could you tell me more detail way to rebuild/replace libgstnvarguscamerasrc.so.
I made jetson os with sdcard flash image .
I’m a begineer.

Hi,
You will see gst-nvarguscamera_src.tbz2 in the source code package. Please copy it to Nano and follow steps in README.

Hi.
Thanks! It works!

1 Like

hello, * Is this bug officially fixed in any version?

Hi,

The latest JP4.5(r32.5) contains the patch.

OK ,Thank you.