[libcamera-devel,v2,3/3] test: gstreamer: Simplify elements' ownerships
diff mbox series

Message ID 20210921173955.20408-3-vedantparanjape160201@gmail.com
State Accepted
Commit c7daf645d449fc74415770b9d811ec21571e7823
Headers show
Series
  • [libcamera-devel,v2,1/3] test: gstreamer_single_stream_test: Fix memory leak
Related show

Commit Message

Vedant Paranjape Sept. 21, 2021, 5:39 p.m. UTC
In gstreamer, when elements are created, usually a floating [1]
reference is returned which simply means, there is no ownership
transfer (yet). Once can simply check for NULL and return through
an error path, without bothering to clean up. Hence, g_autoptr is
not much of help here.

If the NULL checks have been passed successfully, elements are ready
to use. However, we must claim ownership/reference it before using
them via g_object_ref_sink().

This patch build upon this principle and removes the g_autoptr
from gstreamer test base class (gstreamer_test.cpp) whereever
necessary to tide up the code.

[1] https://gstreamer.freedesktop.org/documentation/additional/design/MT-refcounting.html?gi-language=c#refcounting1

Signed-off-by: Vedant Paranjape <vedantparanjape160201@gmail.com>
Signed-off-by: Umang Jain <umang.jain@ideasonboard.com>
Reviewed-by: Nicolas Dufresne <nicolas.dufresne@collabora.com>
---
 test/gstreamer/gstreamer_test.cpp | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

Comments

Paul Elder Sept. 22, 2021, 4:56 a.m. UTC | #1
Hi Vedant,

On Tue, Sep 21, 2021 at 11:09:55PM +0530, Vedant Paranjape wrote:
> In gstreamer, when elements are created, usually a floating [1]
> reference is returned which simply means, there is no ownership
> transfer (yet). Once can simply check for NULL and return through
> an error path, without bothering to clean up. Hence, g_autoptr is
> not much of help here.
> 
> If the NULL checks have been passed successfully, elements are ready
> to use. However, we must claim ownership/reference it before using
> them via g_object_ref_sink().
> 
> This patch build upon this principle and removes the g_autoptr
> from gstreamer test base class (gstreamer_test.cpp) whereever
> necessary to tide up the code.
> 
> [1] https://gstreamer.freedesktop.org/documentation/additional/design/MT-refcounting.html?gi-language=c#refcounting1
> 
> Signed-off-by: Vedant Paranjape <vedantparanjape160201@gmail.com>
> Signed-off-by: Umang Jain <umang.jain@ideasonboard.com>
> Reviewed-by: Nicolas Dufresne <nicolas.dufresne@collabora.com>

Reviewed-by: Paul Elder <paul.elder@ideasonboard.com>

> ---
>  test/gstreamer/gstreamer_test.cpp | 9 ++++-----
>  1 file changed, 4 insertions(+), 5 deletions(-)
> 
> diff --git a/test/gstreamer/gstreamer_test.cpp b/test/gstreamer/gstreamer_test.cpp
> index 4171250584e6..227a5c37fbe1 100644
> --- a/test/gstreamer/gstreamer_test.cpp
> +++ b/test/gstreamer/gstreamer_test.cpp
> @@ -78,18 +78,17 @@ GstreamerTest::~GstreamerTest()
>  
>  int GstreamerTest::createPipeline()
>  {
> -	g_autoptr(GstElement) libcameraSrc = gst_element_factory_make("libcamerasrc", "libcamera");
> +	libcameraSrc_ = gst_element_factory_make("libcamerasrc", "libcamera");
>  	pipeline_ = gst_pipeline_new("test-pipeline");
> -	g_object_ref_sink(libcameraSrc);
>  
> -	if (!libcameraSrc || !pipeline_) {
> +	if (!libcameraSrc_ || !pipeline_) {
>  		g_printerr("Unable to create create pipeline %p.%p\n",
> -			   libcameraSrc, pipeline_);
> +			   libcameraSrc_, pipeline_);
>  
>  		return TestFail;
>  	}
>  
> -	libcameraSrc_ = reinterpret_cast<GstElement *>(g_steal_pointer(&libcameraSrc));
> +	g_object_ref_sink(libcameraSrc_);
>  
>  	return TestPass;
>  }
> -- 
> 2.25.1
>

Patch
diff mbox series

diff --git a/test/gstreamer/gstreamer_test.cpp b/test/gstreamer/gstreamer_test.cpp
index 4171250584e6..227a5c37fbe1 100644
--- a/test/gstreamer/gstreamer_test.cpp
+++ b/test/gstreamer/gstreamer_test.cpp
@@ -78,18 +78,17 @@  GstreamerTest::~GstreamerTest()
 
 int GstreamerTest::createPipeline()
 {
-	g_autoptr(GstElement) libcameraSrc = gst_element_factory_make("libcamerasrc", "libcamera");
+	libcameraSrc_ = gst_element_factory_make("libcamerasrc", "libcamera");
 	pipeline_ = gst_pipeline_new("test-pipeline");
-	g_object_ref_sink(libcameraSrc);
 
-	if (!libcameraSrc || !pipeline_) {
+	if (!libcameraSrc_ || !pipeline_) {
 		g_printerr("Unable to create create pipeline %p.%p\n",
-			   libcameraSrc, pipeline_);
+			   libcameraSrc_, pipeline_);
 
 		return TestFail;
 	}
 
-	libcameraSrc_ = reinterpret_cast<GstElement *>(g_steal_pointer(&libcameraSrc));
+	g_object_ref_sink(libcameraSrc_);
 
 	return TestPass;
 }