From 2b3993b008016eef6752024474eb8a45545caf3f Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Thu, 31 Dec 2020 00:55:56 -0700 Subject: [PATCH] LibThread: Hide Thread's constructor, as it is a Core::Object Just constructing one of these guys on the stack willy nilly will leak the first reference to them. There might be other C_OBJECTs that have public constructors, seems like a good place for some static analysis checks :). Force users to call the construct() method for it. --- Applications/Piano/main.cpp | 4 ++-- DevTools/HackStudio/HackStudioWidget.cpp | 2 +- Libraries/LibThread/Thread.h | 2 +- Services/AudioServer/Mixer.cpp | 6 +++--- Services/AudioServer/Mixer.h | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Applications/Piano/main.cpp b/Applications/Piano/main.cpp index 548bd7eb40..121f1eec0d 100644 --- a/Applications/Piano/main.cpp +++ b/Applications/Piano/main.cpp @@ -74,7 +74,7 @@ int main(int argc, char** argv) Optional save_path; bool need_to_write_wav = false; - LibThread::Thread audio_thread([&] { + auto audio_thread = LibThread::Thread::construct([&] { auto audio = Core::File::construct("/dev/audio"); if (!audio->open(Core::IODevice::WriteOnly)) { dbgln("Can't open audio device: {}", audio->error_string()); @@ -102,7 +102,7 @@ int main(int argc, char** argv) } } }); - audio_thread.start(); + audio_thread->start(); auto menubar = GUI::MenuBar::construct(); diff --git a/DevTools/HackStudio/HackStudioWidget.cpp b/DevTools/HackStudio/HackStudioWidget.cpp index 0fed336232..f1ca53cf9a 100644 --- a/DevTools/HackStudio/HackStudioWidget.cpp +++ b/DevTools/HackStudio/HackStudioWidget.cpp @@ -512,7 +512,7 @@ NonnullRefPtr HackStudioWidget::create_debug_action() } Debugger::the().set_executable_path(get_project_executable_path()); - m_debugger_thread = adopt(*new LibThread::Thread(Debugger::start_static)); + m_debugger_thread = LibThread::Thread::construct(Debugger::start_static); m_debugger_thread->start(); }); } diff --git a/Libraries/LibThread/Thread.h b/Libraries/LibThread/Thread.h index 26ef07d35d..28fba0d78a 100644 --- a/Libraries/LibThread/Thread.h +++ b/Libraries/LibThread/Thread.h @@ -37,7 +37,6 @@ class Thread final : public Core::Object { C_OBJECT(Thread); public: - explicit Thread(Function action, StringView thread_name = nullptr); virtual ~Thread(); void start(); @@ -46,6 +45,7 @@ public: pthread_t tid() const { return m_tid; } private: + explicit Thread(Function action, StringView thread_name = nullptr); Function m_action; pthread_t m_tid { 0 }; String m_thread_name; diff --git a/Services/AudioServer/Mixer.cpp b/Services/AudioServer/Mixer.cpp index 894897cf63..945d0bb13b 100644 --- a/Services/AudioServer/Mixer.cpp +++ b/Services/AudioServer/Mixer.cpp @@ -35,12 +35,12 @@ namespace AudioServer { Mixer::Mixer() : m_device(Core::File::construct("/dev/audio", this)) - , m_sound_thread( + , m_sound_thread(LibThread::Thread::construct( [this] { mix(); return 0; }, - "AudioServer[mixer]") + "AudioServer[mixer]")) { if (!m_device->open(Core::IODevice::WriteOnly)) { dbgprintf("Can't open audio device: %s\n", m_device->error_string()); @@ -52,7 +52,7 @@ Mixer::Mixer() m_zero_filled_buffer = (u8*)malloc(4096); bzero(m_zero_filled_buffer, 4096); - m_sound_thread.start(); + m_sound_thread->start(); } Mixer::~Mixer() diff --git a/Services/AudioServer/Mixer.h b/Services/AudioServer/Mixer.h index fdad64d60a..74fbba6ede 100644 --- a/Services/AudioServer/Mixer.h +++ b/Services/AudioServer/Mixer.h @@ -132,7 +132,7 @@ private: RefPtr m_device; - LibThread::Thread m_sound_thread; + NonnullRefPtr m_sound_thread; bool m_muted { false }; int m_main_volume { 100 };