{"id":10604,"url":"https://patchwork.libcamera.org/api/1.1/patches/10604/?format=json","web_url":"https://patchwork.libcamera.org/patch/10604/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20201208014951.30989-1-laurent.pinchart@ideasonboard.com>","date":"2020-12-08T01:49:50","name":"[libcamera-devel,1/2] libcamera: v4l2_subdevice: Return a unique pointer from fromEntityName()","commit_ref":"ff2ee0174ca62ab4460adfe20049ed05f52615c5","pull_url":null,"state":"accepted","archived":false,"hash":"176ad88361b2b7361971d91c0ccb7efeaa398b29","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/10604/mbox/","series":[{"id":1512,"url":"https://patchwork.libcamera.org/api/1.1/series/1512/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1512","date":"2020-12-08T01:49:50","name":"[libcamera-devel,1/2] libcamera: v4l2_subdevice: Return a unique pointer from fromEntityName()","version":1,"mbox":"https://patchwork.libcamera.org/series/1512/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/10604/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/10604/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id D37E9BDB20\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue,  8 Dec 2020 01:50:33 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 64D0967E6C;\n\tTue,  8 Dec 2020 02:50:33 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 32B3A60323\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  8 Dec 2020 02:50:31 +0100 (CET)","from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id B0E46DD\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  8 Dec 2020 02:50:30 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"bxKuWHzj\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1607392230;\n\tbh=UHhxMC2AlDs4qYTQn+STYpQJDjm3VveaNmiUtfgETP8=;\n\th=From:To:Subject:Date:From;\n\tb=bxKuWHzjfPDjWQsRtV4VoyVVWpyId2bDddzRpbxOlcTX0+mmb7s1OqQCWyY/0Y2tL\n\t1GFYiQ+bR+V8IvqzXuoDmx2W9zTOzPD4d+g1I/qcO2fcBSHg9Z61IATkHJI0kFX5I/\n\twxOZyMEA763u7lh7vaqoxW3itYQ2IC0IaXPXHrC4=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Tue,  8 Dec 2020 03:49:50 +0200","Message-Id":"<20201208014951.30989-1-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.27.0","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH 1/2] libcamera: v4l2_subdevice: Return a\n\tunique pointer from fromEntityName()","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"The fromEntityName() function returns a pointer to a newly allocated\nV4L2Subdevice instance, which must be deleted by the caller. This opens\nthe door to memory leaks. Return a unique pointer instead, which conveys\nthe API semantics better than a sentence in the documentation.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n include/libcamera/internal/v4l2_subdevice.h   |  5 +++--\n src/libcamera/pipeline/ipu3/imgu.cpp          |  2 +-\n src/libcamera/pipeline/rkisp1/rkisp1.cpp      |  6 ++----\n src/libcamera/pipeline/rkisp1/rkisp1_path.cpp |  3 +--\n src/libcamera/pipeline/rkisp1/rkisp1_path.h   |  3 ++-\n src/libcamera/v4l2_subdevice.cpp              | 10 ++++------\n 6 files changed, 13 insertions(+), 16 deletions(-)","diff":"diff --git a/include/libcamera/internal/v4l2_subdevice.h b/include/libcamera/internal/v4l2_subdevice.h\nindex 02ee3e914a8b..eb25fa2fd01b 100644\n--- a/include/libcamera/internal/v4l2_subdevice.h\n+++ b/include/libcamera/internal/v4l2_subdevice.h\n@@ -7,6 +7,7 @@\n #ifndef __LIBCAMERA_INTERNAL_V4L2_SUBDEVICE_H__\n #define __LIBCAMERA_INTERNAL_V4L2_SUBDEVICE_H__\n \n+#include <memory>\n #include <string>\n #include <vector>\n \n@@ -60,8 +61,8 @@ public:\n \tint setFormat(unsigned int pad, V4L2SubdeviceFormat *format,\n \t\t      Whence whence = ActiveFormat);\n \n-\tstatic V4L2Subdevice *fromEntityName(const MediaDevice *media,\n-\t\t\t\t\t     const std::string &entity);\n+\tstatic std::unique_ptr<V4L2Subdevice>\n+\tfromEntityName(const MediaDevice *media, const std::string &entity);\n \n protected:\n \tstd::string logPrefix() const override;\ndiff --git a/src/libcamera/pipeline/ipu3/imgu.cpp b/src/libcamera/pipeline/ipu3/imgu.cpp\nindex a4d74a62f69a..bfe9624c7797 100644\n--- a/src/libcamera/pipeline/ipu3/imgu.cpp\n+++ b/src/libcamera/pipeline/ipu3/imgu.cpp\n@@ -343,7 +343,7 @@ int ImgUDevice::init(MediaDevice *media, unsigned int index)\n \t * by the match() function: no need to check for newly created\n \t * video devices and subdevice validity here.\n \t */\n-\timgu_.reset(V4L2Subdevice::fromEntityName(media, name_));\n+\timgu_ = V4L2Subdevice::fromEntityName(media, name_);\n \tret = imgu_->open();\n \tif (ret)\n \t\treturn ret;\ndiff --git a/src/libcamera/pipeline/rkisp1/rkisp1.cpp b/src/libcamera/pipeline/rkisp1/rkisp1.cpp\nindex 6e74a49abfda..bcfe6c0514ab 100644\n--- a/src/libcamera/pipeline/rkisp1/rkisp1.cpp\n+++ b/src/libcamera/pipeline/rkisp1/rkisp1.cpp\n@@ -217,7 +217,7 @@ private:\n \tint freeBuffers(Camera *camera);\n \n \tMediaDevice *media_;\n-\tV4L2Subdevice *isp_;\n+\tstd::unique_ptr<V4L2Subdevice> isp_;\n \tV4L2VideoDevice *param_;\n \tV4L2VideoDevice *stat_;\n \n@@ -599,8 +599,7 @@ CameraConfiguration::Status RkISP1CameraConfiguration::validate()\n }\n \n PipelineHandlerRkISP1::PipelineHandlerRkISP1(CameraManager *manager)\n-\t: PipelineHandler(manager), isp_(nullptr), param_(nullptr),\n-\t  stat_(nullptr)\n+\t: PipelineHandler(manager), param_(nullptr), stat_(nullptr)\n {\n }\n \n@@ -608,7 +607,6 @@ PipelineHandlerRkISP1::~PipelineHandlerRkISP1()\n {\n \tdelete param_;\n \tdelete stat_;\n-\tdelete isp_;\n }\n \n /* -----------------------------------------------------------------------------\ndiff --git a/src/libcamera/pipeline/rkisp1/rkisp1_path.cpp b/src/libcamera/pipeline/rkisp1/rkisp1_path.cpp\nindex 3f77b1c12f9d..e05d9dd657cd 100644\n--- a/src/libcamera/pipeline/rkisp1/rkisp1_path.cpp\n+++ b/src/libcamera/pipeline/rkisp1/rkisp1_path.cpp\n@@ -24,14 +24,13 @@ RkISP1Path::RkISP1Path(const char *name, const Span<const PixelFormat> &formats,\n \t\t       const Size &minResolution, const Size &maxResolution)\n \t: name_(name), running_(false), formats_(formats),\n \t  minResolution_(minResolution), maxResolution_(maxResolution),\n-\t  resizer_(nullptr), video_(nullptr), link_(nullptr)\n+\t  video_(nullptr), link_(nullptr)\n {\n }\n \n RkISP1Path::~RkISP1Path()\n {\n \tdelete video_;\n-\tdelete resizer_;\n }\n \n bool RkISP1Path::init(MediaDevice *media)\ndiff --git a/src/libcamera/pipeline/rkisp1/rkisp1_path.h b/src/libcamera/pipeline/rkisp1/rkisp1_path.h\nindex 8f443e5179f6..f06ac5a731cc 100644\n--- a/src/libcamera/pipeline/rkisp1/rkisp1_path.h\n+++ b/src/libcamera/pipeline/rkisp1/rkisp1_path.h\n@@ -7,6 +7,7 @@\n #ifndef __LIBCAMERA_PIPELINE_RKISP1_PATH_H__\n #define __LIBCAMERA_PIPELINE_RKISP1_PATH_H__\n \n+#include <memory>\n #include <vector>\n \n #include <libcamera/camera.h>\n@@ -65,7 +66,7 @@ private:\n \tconst Size minResolution_;\n \tconst Size maxResolution_;\n \n-\tV4L2Subdevice *resizer_;\n+\tstd::unique_ptr<V4L2Subdevice> resizer_;\n \tV4L2VideoDevice *video_;\n \tMediaLink *link_;\n };\ndiff --git a/src/libcamera/v4l2_subdevice.cpp b/src/libcamera/v4l2_subdevice.cpp\nindex 85d00c246f5e..721ff5a92a2b 100644\n--- a/src/libcamera/v4l2_subdevice.cpp\n+++ b/src/libcamera/v4l2_subdevice.cpp\n@@ -446,19 +446,17 @@ int V4L2Subdevice::setFormat(unsigned int pad, V4L2SubdeviceFormat *format,\n  * \\param[in] media The media device where the entity is registered\n  * \\param[in] entity The media entity name\n  *\n- * Releasing memory of the newly created instance is responsibility of the\n- * caller of this function.\n- *\n  * \\return A newly created V4L2Subdevice on success, nullptr otherwise\n  */\n-V4L2Subdevice *V4L2Subdevice::fromEntityName(const MediaDevice *media,\n-\t\t\t\t\t     const std::string &entity)\n+std::unique_ptr<V4L2Subdevice>\n+V4L2Subdevice::fromEntityName(const MediaDevice *media,\n+\t\t\t      const std::string &entity)\n {\n \tMediaEntity *mediaEntity = media->getEntityByName(entity);\n \tif (!mediaEntity)\n \t\treturn nullptr;\n \n-\treturn new V4L2Subdevice(mediaEntity);\n+\treturn std::make_unique<V4L2Subdevice>(mediaEntity);\n }\n \n std::string V4L2Subdevice::logPrefix() const\n","prefixes":["libcamera-devel","1/2"]}