From patchwork Sun Aug 23 18:29:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 9374 X-Patchwork-Delegate: laurent.pinchart@ideasonboard.com Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id CAB92BE173 for ; Sun, 23 Aug 2020 18:29:26 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 57BBA61ED9; Sun, 23 Aug 2020 20:29:26 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="LYTOdQxo"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C3D306037F for ; Sun, 23 Aug 2020 20:29:24 +0200 (CEST) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id B5BA5279; Sun, 23 Aug 2020 20:29:21 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1598207361; bh=3OtNntFbc5bgGwg9uxiY3M/yCcy4H7r/DVHylt2SfCw=; h=From:To:Cc:Subject:Date:From; b=LYTOdQxoZJhRA6vYOqIpNVYusBgYNJKewBYMNutkqxLF6kiaOj2EoW6DCB01O98Cl MjGcxHQ1dsmyl//qN/ExjSk8qp/nEfMCsfYuZBjTfb0DqkXSZ/xhbjjNKjFo9e2WLd s1VH/hjlxtLWQ/4FsPp1TnicO48HN3xVHTvcPJgI= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Sun, 23 Aug 2020 21:29:00 +0300 Message-Id: <20200823182900.18247-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH] android: camera_device: Hold reference to self when open X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" CameraDevice instances are managed through shared pointers to support hptplug. No reference is however taken on the device when opened by the camera service, which could cause an open device to be deleted if unplugged, leading to a crash when the device is next accessed. Fix this by taking a reference when opening a device, and releasing it at close time. The reference is stored in the CameraDevice instance itself, which is more efficient than storing it in a container in the CameraHalManager as that would require lookup operations. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- src/android/camera_device.cpp | 12 ++++++++++++ src/android/camera_device.h | 4 +++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index de6f86f7cb9b..52468be70781 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -459,6 +459,12 @@ int CameraDevice::open(const hw_module_t *hardwareModule) camera3Device_.ops = &hal_dev_ops; camera3Device_.priv = this; + /* + * Hold a reference to ourselves, to protect against deletion if the + * camera is unplugged before being closed. + */ + self_ = shared_from_this(); + return 0; } @@ -468,6 +474,12 @@ void CameraDevice::close() camera_->release(); running_ = false; + + /* + * Drop our self reference. From this point the CameraDevice may get + * deleted at any time. + */ + self_.reset(); } void CameraDevice::setCallbacks(const camera3_callback_ops_t *callbacks) diff --git a/src/android/camera_device.h b/src/android/camera_device.h index 3934f194f1b5..fd991c76b90d 100644 --- a/src/android/camera_device.h +++ b/src/android/camera_device.h @@ -44,7 +44,8 @@ struct CameraStream { Encoder *jpeg; }; -class CameraDevice : protected libcamera::Loggable +class CameraDevice : public std::enable_shared_from_this, + protected libcamera::Loggable { public: static std::shared_ptr create(unsigned int id, @@ -104,6 +105,7 @@ private: unsigned int id_; camera3_device_t camera3Device_; + std::shared_ptr self_; bool running_; std::shared_ptr camera_;