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.
This commit is contained in:
Andrew Kaster 2020-12-31 00:55:56 -07:00 committed by Andreas Kling
parent b7fd5315e5
commit 2b3993b008
5 changed files with 8 additions and 8 deletions

View file

@ -74,7 +74,7 @@ int main(int argc, char** argv)
Optional<String> 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();

View file

@ -512,7 +512,7 @@ NonnullRefPtr<GUI::Action> 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();
});
}

View file

@ -37,7 +37,6 @@ class Thread final : public Core::Object {
C_OBJECT(Thread);
public:
explicit Thread(Function<int()> 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<int()> action, StringView thread_name = nullptr);
Function<int()> m_action;
pthread_t m_tid { 0 };
String m_thread_name;

View file

@ -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()

View file

@ -132,7 +132,7 @@ private:
RefPtr<Core::File> m_device;
LibThread::Thread m_sound_thread;
NonnullRefPtr<LibThread::Thread> m_sound_thread;
bool m_muted { false };
int m_main_volume { 100 };