From 9f2820554840d0639aa73dd3c498e56bb676c534 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Mon, 16 Oct 2017 21:13:57 +0200 Subject: [PATCH] Use custom threading wrapper instead of std::thread. std::thread may perform memory allocation when a thread is created (it does so on MSVC). Tracy heap is managed by its own allocator and this changes prevents accessing application heap. --- client/TracyProfiler.cpp | 10 +++++++--- client/TracyProfiler.hpp | 3 +-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/client/TracyProfiler.cpp b/client/TracyProfiler.cpp index 2e088302..86d110d3 100644 --- a/client/TracyProfiler.cpp +++ b/client/TracyProfiler.cpp @@ -23,6 +23,7 @@ #include "TracyAlloc.hpp" #include "TracyScoped.hpp" #include "TracyProfiler.hpp" +#include "TracyThread.hpp" #ifdef _DEBUG # define DISABLE_LZ4 @@ -77,6 +78,7 @@ static Profiler init_order(106) s_profiler; #endif static Profiler* s_instance = nullptr; +static Thread* s_thread = nullptr; Profiler::Profiler() : m_mainThread( GetThreadHandle() ) @@ -94,14 +96,16 @@ Profiler::Profiler() uint32_t cpu; m_timeBegin = GetTime( cpu ); - m_thread = std::thread( [this] { Worker(); } ); - SetThreadName( m_thread, "Tracy Profiler" ); + s_thread = (Thread*)tracy_malloc( sizeof( Thread ) ); + new(s_thread) Thread( LaunchWorker, this ); + SetThreadName( s_thread->Handle(), "Tracy Profiler" ); } Profiler::~Profiler() { m_shutdown.store( true, std::memory_order_relaxed ); - m_thread.join(); + s_thread->~Thread(); + tracy_free( s_thread ); tracy_free( m_buffer ); LZ4_freeStream( m_stream ); diff --git a/client/TracyProfiler.hpp b/client/TracyProfiler.hpp index 58a6876d..b4d4d293 100644 --- a/client/TracyProfiler.hpp +++ b/client/TracyProfiler.hpp @@ -5,7 +5,6 @@ #include #include #include -#include #include "concurrentqueue.h" #include "../common/tracy_lz4.hpp" @@ -163,6 +162,7 @@ public: static bool ShouldExit(); private: + static void LaunchWorker( void* ptr ) { ((Profiler*)ptr)->Worker(); } void Worker(); bool SendData( const char* data, size_t len ); @@ -180,7 +180,6 @@ private: int64_t m_timeBegin; uint64_t m_mainThread; uint64_t m_epoch; - std::thread m_thread; std::atomic m_shutdown; std::unique_ptr m_sock;