[libcamera-devel,v1] test: gstreamer: Fix the destructor of GstreamerTest base class
diff mbox series

Message ID 20210908120420.312959-1-vedantparanjape160201@gmail.com
State Accepted
Headers show
Series
  • [libcamera-devel,v1] test: gstreamer: Fix the destructor of GstreamerTest base class
Related show

Commit Message

Vedant Paranjape Sept. 8, 2021, 12:04 p.m. UTC
The destructor tried to check if pipeline_ is a parent of libcameraSrc_.
This was needed to be checked as if it is, cleanup of libcameraSrc_
would be handled by pipeline itself.

Since, the destructor can be called anytime, even when pipeline_ hasn't
been created, the use of pipeline_ to check if libcameraSrc_ has an
ancestor as pipeline_ caused a segmentation fault.

Fixes: f58768092277 ("test: gstreamer: Fix the destructor of GstreamerTest base class")
Signed-off-by: Vedant Paranjape <vedantparanjape160201@gmail.com>
---
 test/gstreamer/gstreamer_test.cpp | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

Comments

Kieran Bingham Sept. 8, 2021, 1:06 p.m. UTC | #1
On 08/09/2021 13:04, Vedant Paranjape wrote:
> The destructor tried to check if pipeline_ is a parent of libcameraSrc_.
> This was needed to be checked as if it is, cleanup of libcameraSrc_
> would be handled by pipeline itself.
> 
> Since, the destructor can be called anytime, even when pipeline_ hasn't
> been created, the use of pipeline_ to check if libcameraSrc_ has an
> ancestor as pipeline_ caused a segmentation fault.

I presume the refcounting is what actually deals with the cleanup
accordingly now?

Will the gst_object_unref(pipeline_); have any effect such as making
libcameraSrc_ invalid? (because you said that the pipeline handler would

It looks reasonable otherwise:

Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

> 
> Fixes: f58768092277 ("test: gstreamer: Fix the destructor of GstreamerTest base class")
> Signed-off-by: Vedant Paranjape <vedantparanjape160201@gmail.com>
> ---
>  test/gstreamer/gstreamer_test.cpp | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/test/gstreamer/gstreamer_test.cpp b/test/gstreamer/gstreamer_test.cpp
> index dbdcaec0b111..46fa5abaea75 100644
> --- a/test/gstreamer/gstreamer_test.cpp
> +++ b/test/gstreamer/gstreamer_test.cpp
> @@ -69,12 +69,10 @@ GstreamerTest::GstreamerTest()
>  
>  GstreamerTest::~GstreamerTest()
>  {
> -	if (libcameraSrc_ &&
> -	    !gst_object_has_as_ancestor(GST_OBJECT(libcameraSrc_),
> -					GST_OBJECT(pipeline_)))
> -		gst_object_unref(libcameraSrc_);
>  	if (pipeline_)
>  		gst_object_unref(pipeline_);
> +	if (libcameraSrc_)
> +		gst_object_unref(libcameraSrc_);
>  
>  	gst_deinit();
>  }
>
Vedant Paranjape Sept. 8, 2021, 1:20 p.m. UTC | #2
Hi Kieran,

On Wed, Sep 8, 2021 at 6:36 PM Kieran Bingham <
kieran.bingham@ideasonboard.com> wrote:

> On 08/09/2021 13:04, Vedant Paranjape wrote:
> > The destructor tried to check if pipeline_ is a parent of libcameraSrc_.
> > This was needed to be checked as if it is, cleanup of libcameraSrc_
> > would be handled by pipeline itself.
> >
> > Since, the destructor can be called anytime, even when pipeline_ hasn't
> > been created, the use of pipeline_ to check if libcameraSrc_ has an
> > ancestor as pipeline_ caused a segmentation fault.
>
> I presume the refcounting is what actually deals with the cleanup
> accordingly now?
>

Right !

Will the gst_object_unref(pipeline_); have any effect such as making
> libcameraSrc_ invalid? (because you said that the pipeline handler would
>

Yes, it will unref the libcameraSrc_, and I think gst_deinit will free it.
Pinging @Nicolas Dufresne <nicolas@ndufresne.ca> for clearity.

It looks reasonable otherwise:
>
> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
>
> >
> > Fixes: f58768092277 ("test: gstreamer: Fix the destructor of
> GstreamerTest base class")
> > Signed-off-by: Vedant Paranjape <vedantparanjape160201@gmail.com>
> > ---
> >  test/gstreamer/gstreamer_test.cpp | 6 ++----
> >  1 file changed, 2 insertions(+), 4 deletions(-)
> >
> > diff --git a/test/gstreamer/gstreamer_test.cpp
> b/test/gstreamer/gstreamer_test.cpp
> > index dbdcaec0b111..46fa5abaea75 100644
> > --- a/test/gstreamer/gstreamer_test.cpp
> > +++ b/test/gstreamer/gstreamer_test.cpp
> > @@ -69,12 +69,10 @@ GstreamerTest::GstreamerTest()
> >
> >  GstreamerTest::~GstreamerTest()
> >  {
> > -     if (libcameraSrc_ &&
> > -         !gst_object_has_as_ancestor(GST_OBJECT(libcameraSrc_),
> > -                                     GST_OBJECT(pipeline_)))
> > -             gst_object_unref(libcameraSrc_);
> >       if (pipeline_)
> >               gst_object_unref(pipeline_);
> > +     if (libcameraSrc_)
> > +             gst_object_unref(libcameraSrc_);
> >
> >       gst_deinit();
> >  }
> >
>

Regards,
*Vedant Paranjape*
Kieran Bingham Sept. 8, 2021, 1:27 p.m. UTC | #3
On 08/09/2021 14:20, Vedant Paranjape wrote:
> Hi Kieran,
> 
> On Wed, Sep 8, 2021 at 6:36 PM Kieran Bingham
> <kieran.bingham@ideasonboard.com
> <mailto:kieran.bingham@ideasonboard.com>> wrote:
> 
>     On 08/09/2021 13:04, Vedant Paranjape wrote:
>     > The destructor tried to check if pipeline_ is a parent of
>     libcameraSrc_.
>     > This was needed to be checked as if it is, cleanup of libcameraSrc_
>     > would be handled by pipeline itself.
>     >
>     > Since, the destructor can be called anytime, even when pipeline_
>     hasn't
>     > been created, the use of pipeline_ to check if libcameraSrc_ has an
>     > ancestor as pipeline_ caused a segmentation fault.
> 
>     I presume the refcounting is what actually deals with the cleanup
>     accordingly now?
> 
> 
> Right !
> 
>     Will the gst_object_unref(pipeline_); have any effect such as making
>     libcameraSrc_ invalid? (because you said that the pipeline handler would
> 
> 
> Yes, it will unref the libcameraSrc_, and I think gst_deinit will free
> it. Pinging @Nicolas Dufresne <mailto:nicolas@ndufresne.ca> for clearity.

The question is - have you made sure that the refcounting is correctly
balanced, such that this final gst_object_unref(libcameraSrc_);  will be
the one that does the release?

I.e. to make sure it doesn't take the refcount below zero.


> 
>     It looks reasonable otherwise:
> 
>     Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com
>     <mailto:kieran.bingham@ideasonboard.com>>
> 
>     >
>     > Fixes: f58768092277 ("test: gstreamer: Fix the destructor of
>     GstreamerTest base class")
>     > Signed-off-by: Vedant Paranjape <vedantparanjape160201@gmail.com
>     <mailto:vedantparanjape160201@gmail.com>>
>     > ---
>     >  test/gstreamer/gstreamer_test.cpp | 6 ++----
>     >  1 file changed, 2 insertions(+), 4 deletions(-)
>     >
>     > diff --git a/test/gstreamer/gstreamer_test.cpp
>     b/test/gstreamer/gstreamer_test.cpp
>     > index dbdcaec0b111..46fa5abaea75 100644
>     > --- a/test/gstreamer/gstreamer_test.cpp
>     > +++ b/test/gstreamer/gstreamer_test.cpp
>     > @@ -69,12 +69,10 @@ GstreamerTest::GstreamerTest()
>     > 
>     >  GstreamerTest::~GstreamerTest()
>     >  {
>     > -     if (libcameraSrc_ &&
>     > -         !gst_object_has_as_ancestor(GST_OBJECT(libcameraSrc_),
>     > -                                     GST_OBJECT(pipeline_)))
>     > -             gst_object_unref(libcameraSrc_);
>     >       if (pipeline_)
>     >               gst_object_unref(pipeline_);
>     > +     if (libcameraSrc_)
>     > +             gst_object_unref(libcameraSrc_);
>     > 
>     >       gst_deinit();
>     >  }
>     >
> 
> 
> Regards,
> /*Vedant Paranjape*/
Vedant Paranjape Sept. 8, 2021, 1:30 p.m. UTC | #4
Hi Kieran,

On Wed, 8 Sep, 2021, 18:57 Kieran Bingham, <kieran.bingham@ideasonboard.com>
wrote:

> On 08/09/2021 14:20, Vedant Paranjape wrote:
> > Hi Kieran,
> >
> > On Wed, Sep 8, 2021 at 6:36 PM Kieran Bingham
> > <kieran.bingham@ideasonboard.com
> > <mailto:kieran.bingham@ideasonboard.com>> wrote:
> >
> >     On 08/09/2021 13:04, Vedant Paranjape wrote:
> >     > The destructor tried to check if pipeline_ is a parent of
> >     libcameraSrc_.
> >     > This was needed to be checked as if it is, cleanup of libcameraSrc_
> >     > would be handled by pipeline itself.
> >     >
> >     > Since, the destructor can be called anytime, even when pipeline_
> >     hasn't
> >     > been created, the use of pipeline_ to check if libcameraSrc_ has an
> >     > ancestor as pipeline_ caused a segmentation fault.
> >
> >     I presume the refcounting is what actually deals with the cleanup
> >     accordingly now?
> >
> >
> > Right !
> >
> >     Will the gst_object_unref(pipeline_); have any effect such as making
> >     libcameraSrc_ invalid? (because you said that the pipeline handler
> would
> >
> >
> > Yes, it will unref the libcameraSrc_, and I think gst_deinit will free
> > it. Pinging @Nicolas Dufresne <mailto:nicolas@ndufresne.ca> for
> clearity.
>
> The question is - have you made sure that the refcounting is correctly
> balanced, such that this final gst_object_unref(libcameraSrc_);  will be
> the one that does the release?
>
> I.e. to make sure it doesn't take the refcount below zero.
>

It can't go below zero, and this is the only place where I do a unref, so
Yes !


> >
> >     It looks reasonable otherwise:
> >
> >     Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com
> >     <mailto:kieran.bingham@ideasonboard.com>>
> >
> >     >
> >     > Fixes: f58768092277 ("test: gstreamer: Fix the destructor of
> >     GstreamerTest base class")
> >     > Signed-off-by: Vedant Paranjape <vedantparanjape160201@gmail.com
> >     <mailto:vedantparanjape160201@gmail.com>>
> >     > ---
> >     >  test/gstreamer/gstreamer_test.cpp | 6 ++----
> >     >  1 file changed, 2 insertions(+), 4 deletions(-)
> >     >
> >     > diff --git a/test/gstreamer/gstreamer_test.cpp
> >     b/test/gstreamer/gstreamer_test.cpp
> >     > index dbdcaec0b111..46fa5abaea75 100644
> >     > --- a/test/gstreamer/gstreamer_test.cpp
> >     > +++ b/test/gstreamer/gstreamer_test.cpp
> >     > @@ -69,12 +69,10 @@ GstreamerTest::GstreamerTest()
> >     >
> >     >  GstreamerTest::~GstreamerTest()
> >     >  {
> >     > -     if (libcameraSrc_ &&
> >     > -         !gst_object_has_as_ancestor(GST_OBJECT(libcameraSrc_),
> >     > -                                     GST_OBJECT(pipeline_)))
> >     > -             gst_object_unref(libcameraSrc_);
> >     >       if (pipeline_)
> >     >               gst_object_unref(pipeline_);
> >     > +     if (libcameraSrc_)
> >     > +             gst_object_unref(libcameraSrc_);
> >     >
> >     >       gst_deinit();
> >     >  }
> >     >
> >
> >
> > Regards,
> > /*Vedant Paranjape*/
>

Regards,
Vedant Paranjape

>
Nicolas Dufresne Sept. 8, 2021, 1:42 p.m. UTC | #5
Le mercredi 08 septembre 2021 à 17:34 +0530, Vedant Paranjape a écrit :
> The destructor tried to check if pipeline_ is a parent of libcameraSrc_.
> This was needed to be checked as if it is, cleanup of libcameraSrc_
> would be handled by pipeline itself.
> 
> Since, the destructor can be called anytime, even when pipeline_ hasn't
> been created, the use of pipeline_ to check if libcameraSrc_ has an
> ancestor as pipeline_ caused a segmentation fault.

This fix i correct since you do call gst_object_ref_sink() after creating the
libcamerasrc element. Element are created with a floating ref and this floating
ref is taken by gst_bin_add() normally, unless you have called ref_sink(). So
for the correctness:

Reviewed-by: Nicolas Dufresne <nicolas.dufresne@collabora.com>

Though, I still believe the implementation is messy and error prone. I would
rather drop the creation of pipeline_ and libcameraSrc_ in the base class, and
use gst_parse_launch() in the tests themself. This would be much more readable,
faster to implement, and less error prone.

> 
> Fixes: f58768092277 ("test: gstreamer: Fix the destructor of GstreamerTest base class")
> Signed-off-by: Vedant Paranjape <vedantparanjape160201@gmail.com>
> ---
>  test/gstreamer/gstreamer_test.cpp | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/test/gstreamer/gstreamer_test.cpp b/test/gstreamer/gstreamer_test.cpp
> index dbdcaec0b111..46fa5abaea75 100644
> --- a/test/gstreamer/gstreamer_test.cpp
> +++ b/test/gstreamer/gstreamer_test.cpp
> @@ -69,12 +69,10 @@ GstreamerTest::GstreamerTest()
>  
>  GstreamerTest::~GstreamerTest()
>  {
> -	if (libcameraSrc_ &&
> -	    !gst_object_has_as_ancestor(GST_OBJECT(libcameraSrc_),
> -					GST_OBJECT(pipeline_)))
> -		gst_object_unref(libcameraSrc_);
>  	if (pipeline_)
>  		gst_object_unref(pipeline_);
> +	if (libcameraSrc_)
> +		gst_object_unref(libcameraSrc_);
>  
>  	gst_deinit();
>  }

Patch
diff mbox series

diff --git a/test/gstreamer/gstreamer_test.cpp b/test/gstreamer/gstreamer_test.cpp
index dbdcaec0b111..46fa5abaea75 100644
--- a/test/gstreamer/gstreamer_test.cpp
+++ b/test/gstreamer/gstreamer_test.cpp
@@ -69,12 +69,10 @@  GstreamerTest::GstreamerTest()
 
 GstreamerTest::~GstreamerTest()
 {
-	if (libcameraSrc_ &&
-	    !gst_object_has_as_ancestor(GST_OBJECT(libcameraSrc_),
-					GST_OBJECT(pipeline_)))
-		gst_object_unref(libcameraSrc_);
 	if (pipeline_)
 		gst_object_unref(pipeline_);
+	if (libcameraSrc_)
+		gst_object_unref(libcameraSrc_);
 
 	gst_deinit();
 }