Fix memory leak when creating new wxThreads.

See #22840.
This commit is contained in:
Vadim Zeitlin 2022-10-05 00:39:59 +02:00
commit b03cb010ca
5 changed files with 17 additions and 14 deletions

View file

@ -602,9 +602,6 @@ protected:
// of this thread.
virtual void *Entry() = 0;
// use this to call the Entry() virtual method
void *CallEntry();
// Callbacks which may be overridden by the derived class to perform some
// specific actions when the thread is deleted or killed. By default they
// do nothing.

View file

@ -342,16 +342,8 @@ wxSemaError wxSemaphore::Post()
// ----------------------------------------------------------------------------
#include "wx/utils.h"
#include "wx/private/threadinfo.h"
#include "wx/scopeguard.h"
void wxThread::Sleep(unsigned long milliseconds)
{
wxMilliSleep(milliseconds);
}
void *wxThread::CallEntry()
{
wxON_BLOCK_EXIT0(wxThreadSpecificInfo::ThreadCleanUp);
return Entry();
}

View file

@ -125,7 +125,7 @@ void wxThreadPrivate::SprocStart(void *ptr)
thr->p_internal->thread_id = getpid();
thr->p_internal->exit_status = 0;
status = thr->CallEntry();
status = thr->Entry();
thr->Exit(status);
}

View file

@ -32,6 +32,8 @@
#include "wx/apptrait.h"
#include "wx/scopeguard.h"
#include "wx/private/threadinfo.h"
#include "wx/msw/private.h"
#include "wx/msw/missing.h"
#include "wx/msw/seh.h"
@ -532,7 +534,7 @@ THREAD_RETVAL wxThreadInternal::DoThreadStart(wxThread *thread)
return THREAD_ERROR_EXIT;
}
rc = wxPtrToUInt(thread->CallEntry());
rc = wxPtrToUInt(thread->Entry());
}
wxCATCH_ALL( wxTheApp->OnUnhandledException(); )
@ -583,6 +585,10 @@ THREAD_RETVAL THREAD_CALLCONV wxThreadInternal::WinThreadStart(void *param)
if ( isDetached )
thread->m_internal->LetDie();
// Do this as the very last thing to ensure that thread-specific info is
// not recreated any longer.
wxThreadSpecificInfo::ThreadCleanUp();
return rc;
}

View file

@ -27,6 +27,9 @@
#include "wx/thread.h"
#include "wx/except.h"
#include "wx/scopeguard.h"
#include "wx/private/threadinfo.h"
#ifndef WX_PRECOMP
#include "wx/app.h"
@ -855,6 +858,11 @@ void *wxPthreadStart(void *ptr)
void *wxThreadInternal::PthreadStart(wxThread *thread)
{
// Ensure that we clean up thread-specific data before exiting the thread
// and do it as late as possible as wxLog calls can recreate it and may
// happen until the very end.
wxON_BLOCK_EXIT0(wxThreadSpecificInfo::ThreadCleanUp);
wxThreadInternal *pthread = thread->m_internal;
wxLogTrace(TRACE_THREADS, wxT("Thread %p started."), THR_ID(pthread));
@ -900,7 +908,7 @@ void *wxThreadInternal::PthreadStart(wxThread *thread)
wxTRY
{
pthread->m_exitcode = thread->CallEntry();
pthread->m_exitcode = thread->Entry();
wxLogTrace(TRACE_THREADS,
wxT("Thread %p Entry() returned %lu."),