[libcamera-devel,SimpleCam,1/4] simple-cam: Use a unique_ptr for the CameraManager
diff mbox series

Message ID 20210824142450.3157833-2-kieran.bingham@ideasonboard.com
State Accepted
Headers show
Series
  • Improve CameraManager usage and Doc
Related show

Commit Message

Kieran Bingham Aug. 24, 2021, 2:24 p.m. UTC
The CameraManager should be released when it is no longer used. A
unique_ptr will handle this automatically, and convey the lifetime of
the object.

Update simple-cam to show that managing the lifetime of the
CameraManager is recommended practice.

Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
---
 simple-cam.cpp | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

Comments

Jacopo Mondi Aug. 25, 2021, 9:07 a.m. UTC | #1
Hi Kieran,

On Tue, Aug 24, 2021 at 03:24:47PM +0100, Kieran Bingham wrote:
> The CameraManager should be released when it is no longer used. A
> unique_ptr will handle this automatically, and convey the lifetime of
> the object.
>
> Update simple-cam to show that managing the lifetime of the
> CameraManager is recommended practice.
>
> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
> ---
>  simple-cam.cpp | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/simple-cam.cpp b/simple-cam.cpp
> index 8f7012b83b6c..5c16db67700c 100644
> --- a/simple-cam.cpp
> +++ b/simple-cam.cpp
> @@ -131,8 +131,15 @@ int main()
>  	 *
>  	 * The CameraManager provides a list of available Cameras that
>  	 * applications can operate on.
> +	 *
> +	 * When the CameraManager is no longer to be used, it should be deleted.
> +	 * We use a unique_ptr here to manage the lifetime automatically during
> +	 * the scope of this function.
> +	 *
> +	 * There can only be a single CameraManager constructed within any
> +	 * process space.
>  	 */
> -	CameraManager *cm = new CameraManager();
> +	std::unique_ptr<CameraManager> cm = std::make_unique<CameraManager>();

Assuming there are no 'delete cm' leftovers in the code base
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>

Thanks
   j

>  	cm->start();
>
>  	/*
> --
> 2.30.2
>
Kieran Bingham Aug. 25, 2021, 9:44 a.m. UTC | #2
Hi JM,

On 25/08/2021 10:07, Jacopo Mondi wrote:
> Hi Kieran,
> 
> On Tue, Aug 24, 2021 at 03:24:47PM +0100, Kieran Bingham wrote:
>> The CameraManager should be released when it is no longer used. A
>> unique_ptr will handle this automatically, and convey the lifetime of
>> the object.
>>
>> Update simple-cam to show that managing the lifetime of the
>> CameraManager is recommended practice.
>>
>> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
>> ---
>>  simple-cam.cpp | 9 ++++++++-
>>  1 file changed, 8 insertions(+), 1 deletion(-)
>>
>> diff --git a/simple-cam.cpp b/simple-cam.cpp
>> index 8f7012b83b6c..5c16db67700c 100644
>> --- a/simple-cam.cpp
>> +++ b/simple-cam.cpp
>> @@ -131,8 +131,15 @@ int main()
>>  	 *
>>  	 * The CameraManager provides a list of available Cameras that
>>  	 * applications can operate on.
>> +	 *
>> +	 * When the CameraManager is no longer to be used, it should be deleted.
>> +	 * We use a unique_ptr here to manage the lifetime automatically during
>> +	 * the scope of this function.
>> +	 *
>> +	 * There can only be a single CameraManager constructed within any
>> +	 * process space.
>>  	 */
>> -	CameraManager *cm = new CameraManager();
>> +	std::unique_ptr<CameraManager> cm = std::make_unique<CameraManager>();
> 
> Assuming there are no 'delete cm' leftovers in the code base

There are not. Previously it was leaking (though right at the end before
the application closed, so it didn't matter too much)

> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>

Thanks

> 
> Thanks
>    j
> 
>>  	cm->start();
>>
>>  	/*
>> --
>> 2.30.2
>>
Laurent Pinchart Aug. 25, 2021, 7:58 p.m. UTC | #3
Hi Kieran,

Thank you for the patch.

On Tue, Aug 24, 2021 at 03:24:47PM +0100, Kieran Bingham wrote:
> The CameraManager should be released when it is no longer used. A
> unique_ptr will handle this automatically, and convey the lifetime of
> the object.
> 
> Update simple-cam to show that managing the lifetime of the
> CameraManager is recommended practice.
> 
> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> ---
>  simple-cam.cpp | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
> 
> diff --git a/simple-cam.cpp b/simple-cam.cpp
> index 8f7012b83b6c..5c16db67700c 100644
> --- a/simple-cam.cpp
> +++ b/simple-cam.cpp
> @@ -131,8 +131,15 @@ int main()
>  	 *
>  	 * The CameraManager provides a list of available Cameras that
>  	 * applications can operate on.
> +	 *
> +	 * When the CameraManager is no longer to be used, it should be deleted.
> +	 * We use a unique_ptr here to manage the lifetime automatically during
> +	 * the scope of this function.
> +	 *
> +	 * There can only be a single CameraManager constructed within any
> +	 * process space.
>  	 */
> -	CameraManager *cm = new CameraManager();
> +	std::unique_ptr<CameraManager> cm = std::make_unique<CameraManager>();
>  	cm->start();
>  
>  	/*

Patch
diff mbox series

diff --git a/simple-cam.cpp b/simple-cam.cpp
index 8f7012b83b6c..5c16db67700c 100644
--- a/simple-cam.cpp
+++ b/simple-cam.cpp
@@ -131,8 +131,15 @@  int main()
 	 *
 	 * The CameraManager provides a list of available Cameras that
 	 * applications can operate on.
+	 *
+	 * When the CameraManager is no longer to be used, it should be deleted.
+	 * We use a unique_ptr here to manage the lifetime automatically during
+	 * the scope of this function.
+	 *
+	 * There can only be a single CameraManager constructed within any
+	 * process space.
 	 */
-	CameraManager *cm = new CameraManager();
+	std::unique_ptr<CameraManager> cm = std::make_unique<CameraManager>();
 	cm->start();
 
 	/*