From patchwork Fri Jul 31 13:39:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 9090 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 0E1CDBD878 for ; Fri, 31 Jul 2020 13:40:00 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 8EB8761EEF; Fri, 31 Jul 2020 15:39:59 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=uajain.com header.i=@uajain.com header.b="E8HMt7yo"; dkim-atps=neutral Received: from o1.f.az.sendgrid.net (o1.f.az.sendgrid.net [208.117.55.132]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id EAA9D61EA4 for ; Fri, 31 Jul 2020 15:39:56 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uajain.com; h=from:subject:in-reply-to:references:mime-version:to:cc: content-transfer-encoding:content-type; s=s1; bh=Q13Y/KIA9jXepafD4MdInIVxJgHdpkJml9AgdG7ctRo=; b=E8HMt7yosv/O5SF43DjkQoJGKN62gtxVzVP1o1zXsDka6fvrK+gLf4WUdmYaxFmhAmO1 8JJuuHGysh31NZZvQm1jwXeT/F8AGDi/k0N7TAvzgSdqJL9ptYF4ZL/Gp/FLBHgrvIGh1l YgBDhFrrpmA/R0XX8a9wkkztquqm0MXNI= Received: by filterdrecv-p3iad2-d8cc6d457-tlvzw with SMTP id filterdrecv-p3iad2-d8cc6d457-tlvzw-19-5F241F2B-2A 2020-07-31 13:39:55.67984757 +0000 UTC m=+156690.394921146 Received: from mail.uajain.com (unknown) by ismtpd0001p1maa1.sendgrid.net (SG) with ESMTP id APo5bF33SdSYpXkNlB5ZcQ Fri, 31 Jul 2020 13:39:55.235 +0000 (UTC) From: Umang Jain Date: Fri, 31 Jul 2020 13:39:55 +0000 (UTC) Message-Id: <20200731133947.84258-4-email@uajain.com> In-Reply-To: <20200731133947.84258-1-email@uajain.com> References: <20200731133947.84258-1-email@uajain.com> Mime-Version: 1.0 X-SG-EID: 1Q40EQ7YGir8a9gjSIAdTjhngY657NMk9ckeo4dbHZDiOpywc/L3L9rFqlwE4KPcREc2X1LpIgLW8oEasGbeg9uPR3HyoPdWA+c8k6xqhnH2yVbMfbMS9MDZuibMw2RmskFzVjMwpfjjA/Ah5AKcr1cMsEQlRCK6Hj8SG1mFRxAHATDOM1J51AiekMHBcBKn6ixU7B4tO1QcPQByENNmqq2CNN0E1GKA4oYfagZAufxjlObs93wSJLD9Ej+mRWZqndUvlHrTBeJdB24nqjqXZA== To: libcamera-devel@lists.libcamera.org Subject: [libcamera-devel] [PATCH v3 3/3] libcamera: camera: Ensure deletion via deleteLater() 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" Object::deleteLater() ensures that the deletion of the Object takes place in a thread it is bound to. Deleting the Object in a different thread is a violation according to the threading model. On hot-unplug of a currently streaming camera, the last reference of Camera when dropped from the application thread (for e.g. QCam's thread), the destructor is then called from this thread. This is not allowed by the threading model. Camera is meant to be deleted in the thread it is bound to - in this case the CameraManager's thread. Signed-off-by: Umang Jain Reviewed-by: Laurent Pinchart --- include/libcamera/camera.h | 3 ++- src/libcamera/camera.cpp | 2 +- src/libcamera/camera_manager.cpp | 6 +++++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/include/libcamera/camera.h b/include/libcamera/camera.h index 4d1a4a9..7dd23d7 100644 --- a/include/libcamera/camera.h +++ b/include/libcamera/camera.h @@ -13,6 +13,7 @@ #include #include +#include #include #include #include @@ -66,7 +67,7 @@ protected: std::vector config_; }; -class Camera final : public std::enable_shared_from_this +class Camera final : public Object, public std::enable_shared_from_this { public: static std::shared_ptr create(PipelineHandler *pipe, diff --git a/src/libcamera/camera.cpp b/src/libcamera/camera.cpp index 69a1b44..034f341 100644 --- a/src/libcamera/camera.cpp +++ b/src/libcamera/camera.cpp @@ -464,7 +464,7 @@ std::shared_ptr Camera::create(PipelineHandler *pipe, struct Deleter : std::default_delete { void operator()(Camera *camera) { - delete camera; + camera->deleteLater(); } }; diff --git a/src/libcamera/camera_manager.cpp b/src/libcamera/camera_manager.cpp index f60491d..c45bf33 100644 --- a/src/libcamera/camera_manager.cpp +++ b/src/libcamera/camera_manager.cpp @@ -164,9 +164,13 @@ void CameraManager::Private::cleanup() /* * Release all references to cameras to ensure they all get destroyed - * before the device enumerator deletes the media devices. + * before the device enumerator deletes the media devices. Cameras are + * destroyed via Object::deleteLater() API, hence we need to explicitly + * process deletion requests from the thread's message queue as the event + * loop is not in action here. */ cameras_.clear(); + dispatchMessages(Message::Type::DeferredDelete); enumerator_.reset(nullptr); }