From patchwork Fri Jul 31 10:53:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 9086 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 DAFD8BD86F for ; Fri, 31 Jul 2020 10:53:48 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 9927061EAB; Fri, 31 Jul 2020 12:53:48 +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="rTV8zgL4"; 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 4CD8061DC3 for ; Fri, 31 Jul 2020 12:53:44 +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=rTV8zgL4Kjur1x6RTQqEG8WDYxUOlT2iM3gZQ9nmE0x87jKDASt9mh036xAX3FcTZOBc GzGKlCXR8WocbZMjeq8bpUTgFZExfwEXYXn6HXBRSoP680/Z/zFc6SNR/qTXmqQdRfyFNI lNfgHUFIXDS1Lln5+SCMhkMq03y2JOuCc= Received: by filterdrecv-p3mdw1-7ff865655c-g882x with SMTP id filterdrecv-p3mdw1-7ff865655c-g882x-20-5F23F836-56 2020-07-31 10:53:43.011386453 +0000 UTC m=+146252.489555543 Received: from mail.uajain.com (unknown) by ismtpd0005p1hnd1.sendgrid.net (SG) with ESMTP id W0LrwjoTRy25EnBiyiVl2w Fri, 31 Jul 2020 10:53:42.688 +0000 (UTC) From: Umang Jain Date: Fri, 31 Jul 2020 10:53:43 +0000 (UTC) Message-Id: <20200731105335.62014-4-email@uajain.com> In-Reply-To: <20200731105335.62014-1-email@uajain.com> References: <20200731105335.62014-1-email@uajain.com> Mime-Version: 1.0 X-SG-EID: 1Q40EQ7YGir8a9gjSIAdTjhngY657NMk9ckeo4dbHZDiOpywc/L3L9rFqlwE4KPcsL0v8VFFOnVCltD6I338jMD1MLjIeeAEM88dkYf60Kf3IBk5AwVG5GFoahk93LIUADk+7bCK7RTdEj/tdw6Ymf4UobQYlils4p1Xvqa3IvdZh50MHwRguXp98KzVR4KkTvcGyqei/wGQ7lMLe0uGRToa75BFG88HFwbmsHIS4KoJ9aKvgVXQ5VI8KY55C4TAADCbaMkj5pj7DtQgXtDtDw== To: libcamera-devel@lists.libcamera.org Subject: [libcamera-devel] [PATCH v2 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); }