libcamera: v4l2_device: Return a unique pointer from fromEntityName()

The fromEntityName() function returns a pointer to a newly allocated
V4L2Device instance, which must be deleted by the caller. This opens the
door to memory leaks. Return a unique pointer instead, which conveys the
API semantics better than a sentence in the documentation.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
This commit is contained in:
Laurent Pinchart
2020-12-08 03:40:25 +02:00
parent ff2ee0174c
commit e638ffde53
9 changed files with 18 additions and 36 deletions

View File

@@ -1598,19 +1598,17 @@ int V4L2VideoDevice::streamOff()
* \param[in] media The media device where the entity is registered
* \param[in] entity The media entity name
*
* Releasing memory of the newly created instance is responsibility of the
* caller of this function.
*
* \return A newly created V4L2VideoDevice on success, nullptr otherwise
*/
V4L2VideoDevice *V4L2VideoDevice::fromEntityName(const MediaDevice *media,
const std::string &entity)
std::unique_ptr<V4L2VideoDevice>
V4L2VideoDevice::fromEntityName(const MediaDevice *media,
const std::string &entity)
{
MediaEntity *mediaEntity = media->getEntityByName(entity);
if (!mediaEntity)
return nullptr;
return new V4L2VideoDevice(mediaEntity);
return std::make_unique<V4L2VideoDevice>(mediaEntity);
}
/**