From 05b80226010bef5d296ed8331a0582a6e045a4ae Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Sun, 11 Apr 2021 20:12:40 +0300 Subject: test: threads: Fix memory leak The last instance of Thread allocated in the test is never deleted, nor are other instances deleted in error paths. Use a std::unique_ptr<> to ensure deletion. Signed-off-by: Laurent Pinchart Reviewed-by: Umang Jain Tested-by: Sebastian Fricke --- test/threads.cpp | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'test') diff --git a/test/threads.cpp b/test/threads.cpp index b4b8d913..e0c50dc9 100644 --- a/test/threads.cpp +++ b/test/threads.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include "libcamera/internal/thread.h" @@ -45,23 +46,23 @@ protected: int run() { /* Test Thread() retrieval for the main thread. */ - Thread *thread = Thread::current(); - if (!thread) { + Thread *mainThread = Thread::current(); + if (!mainThread) { cout << "Thread::current() failed to main thread" << endl; return TestFail; } - if (!thread->isRunning()) { + if (!mainThread->isRunning()) { cout << "Main thread is not running" << endl; return TestFail; } /* Test starting the main thread, the test shall not crash. */ - thread->start(); + mainThread->start(); /* Test the running state of a custom thread. */ - thread = new Thread(); + std::unique_ptr thread = std::make_unique(); thread->start(); if (!thread->isRunning()) { @@ -79,10 +80,8 @@ protected: return TestFail; } - delete thread; - /* Test waiting for completion with a timeout. */ - thread = new DelayThread(chrono::milliseconds(500)); + thread = std::make_unique(chrono::milliseconds(500)); thread->start(); thread->exit(0); @@ -100,10 +99,8 @@ protected: return TestFail; } - delete thread; - /* Test waiting on a thread that isn't running. */ - thread = new Thread(); + thread = std::make_unique(); timeout = !thread->wait(); if (timeout) { -- cgit v1.2.1