From patchwork Tue Jan 23 01:12:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 19458 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 737E1C32C3 for ; Tue, 23 Jan 2024 01:13:03 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 164F46294F; Tue, 23 Jan 2024 02:13:03 +0100 (CET) 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="EPVvuIgQ"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 78A926297F for ; Tue, 23 Jan 2024 02:12:59 +0100 (CET) Received: from pendragon.ideasonboard.com (89-27-53-110.bb.dnainternet.fi [89.27.53.110]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 23690E4; Tue, 23 Jan 2024 02:11:46 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1705972306; bh=4VK46g9gxjQEAtaVW9zeOvVEgCmYZf55aXihFgS9XTM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EPVvuIgQt+EMk2JlswtN83qGO3l2SNFSScizZ4lxxHXntCBmZaLNtBJsN5CzVv2Sk oVkCk5kNQ2J54/F1P1YTkCCsnPn/K2zUCr5dc2OJch6Tr+eL9BXwt8te5RBNB5ptYt YEZJIdNMxCGAcLTGfqUzX6Fms+O2qPuxzQeFaZm4= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Subject: [PATCH v2 10/12] test: timer-thread: Destroy Object from correct thread context Date: Tue, 23 Jan 2024 03:12:47 +0200 Message-ID: <20240123011249.22716-11-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240123011249.22716-1-laurent.pinchart@ideasonboard.com> References: <20240123011249.22716-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 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" The TimeoutHandler used in the test is destroyed from the main thread, which is invalid for a thread-bound object bound to a different thread. Fix it by destroying it with deleteLater(). Signed-off-by: Laurent Pinchart Reviewed-by: Milan Zamazal --- test/timer-fail.cpp | 16 +++++++++++----- test/timer-thread.cpp | 14 ++++++++++---- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/test/timer-fail.cpp b/test/timer-fail.cpp index 2c622ca3410d..82854b89630d 100644 --- a/test/timer-fail.cpp +++ b/test/timer-fail.cpp @@ -54,7 +54,9 @@ protected: int init() { thread_.start(); - timeout_.moveToThread(&thread_); + + timeout_ = new TimeoutHandler(); + timeout_->moveToThread(&thread_); return TestPass; } @@ -67,7 +69,7 @@ protected: * event dispatcher to make sure we don't succeed simply because * the event dispatcher hasn't noticed the timer restart. */ - timeout_.start(); + timeout_->start(); thread_.eventDispatcher()->interrupt(); this_thread::sleep_for(chrono::milliseconds(200)); @@ -79,7 +81,7 @@ protected: * to return TestPass in the unexpected (usually known as * "fail") case, and TestFail otherwise. */ - if (timeout_.timeout()) { + if (timeout_->timeout()) { cout << "Timer start from wrong thread succeeded unexpectedly" << endl; return TestPass; @@ -90,13 +92,17 @@ protected: void cleanup() { - /* Must stop thread before destroying timeout. */ + /* + * Object class instances must be destroyed from the thread + * they live in. + */ + timeout_->deleteLater(); thread_.exit(0); thread_.wait(); } private: - TimeoutHandler timeout_; + TimeoutHandler *timeout_; Thread thread_; }; diff --git a/test/timer-thread.cpp b/test/timer-thread.cpp index 4caf4e33b2ba..8675e2480aa9 100644 --- a/test/timer-thread.cpp +++ b/test/timer-thread.cpp @@ -50,7 +50,9 @@ protected: int init() { thread_.start(); - timeout_.moveToThread(&thread_); + + timeout_ = new TimeoutHandler(); + timeout_->moveToThread(&thread_); return TestPass; } @@ -63,7 +65,7 @@ protected: */ this_thread::sleep_for(chrono::milliseconds(200)); - if (!timeout_.timeout()) { + if (!timeout_->timeout()) { cout << "Timer expiration test failed" << endl; return TestFail; } @@ -73,13 +75,17 @@ protected: void cleanup() { - /* Must stop thread before destroying timeout. */ + /* + * Object class instances must be destroyed from the thread + * they live in. + */ + timeout_->deleteLater(); thread_.exit(0); thread_.wait(); } private: - TimeoutHandler timeout_; + TimeoutHandler *timeout_; Thread thread_; };