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 <laurent.pinchart@ideasonboard.com>
Reviewed-by: Umang Jain <umang.jain@ideasonboard.com>
Tested-by: Sebastian Fricke <sebastian.fricke@posteo.net>
This commit is contained in:
Laurent Pinchart 2021-04-11 20:12:40 +03:00
parent 9b2544d3df
commit 05b8022601

View file

@ -7,6 +7,7 @@
#include <chrono> #include <chrono>
#include <iostream> #include <iostream>
#include <memory>
#include <thread> #include <thread>
#include "libcamera/internal/thread.h" #include "libcamera/internal/thread.h"
@ -45,23 +46,23 @@ protected:
int run() int run()
{ {
/* Test Thread() retrieval for the main thread. */ /* Test Thread() retrieval for the main thread. */
Thread *thread = Thread::current(); Thread *mainThread = Thread::current();
if (!thread) { if (!mainThread) {
cout << "Thread::current() failed to main thread" cout << "Thread::current() failed to main thread"
<< endl; << endl;
return TestFail; return TestFail;
} }
if (!thread->isRunning()) { if (!mainThread->isRunning()) {
cout << "Main thread is not running" << endl; cout << "Main thread is not running" << endl;
return TestFail; return TestFail;
} }
/* Test starting the main thread, the test shall not crash. */ /* Test starting the main thread, the test shall not crash. */
thread->start(); mainThread->start();
/* Test the running state of a custom thread. */ /* Test the running state of a custom thread. */
thread = new Thread(); std::unique_ptr<Thread> thread = std::make_unique<Thread>();
thread->start(); thread->start();
if (!thread->isRunning()) { if (!thread->isRunning()) {
@ -79,10 +80,8 @@ protected:
return TestFail; return TestFail;
} }
delete thread;
/* Test waiting for completion with a timeout. */ /* Test waiting for completion with a timeout. */
thread = new DelayThread(chrono::milliseconds(500)); thread = std::make_unique<DelayThread>(chrono::milliseconds(500));
thread->start(); thread->start();
thread->exit(0); thread->exit(0);
@ -100,10 +99,8 @@ protected:
return TestFail; return TestFail;
} }
delete thread;
/* Test waiting on a thread that isn't running. */ /* Test waiting on a thread that isn't running. */
thread = new Thread(); thread = std::make_unique<Thread>();
timeout = !thread->wait(); timeout = !thread->wait();
if (timeout) { if (timeout) {