Fix crash when using wxNotebook with glib 2.73 or later

Make GTK notebook log suppression opt-in to avoid a fatal error when the
application calls g_log_set_writer_func() itself, as doing it more than
once immediately kills the application with glib 2.73 and there is no
way to check if it had been already done or not (you have to admire the
purity of the API design here).

This is unfortunate as 99% of the wxWidgets applications that do _not_
call g_log_set_writer_func() would now show the spurious diagnostics by
default again, but preferable to making the remaining 1% crash, and
there doesn't seem to be any other solution.

Call the new GTKAllowDiagnosticsControl() function in the notebook
sample to at least still avoid getting the spurious diagnostic messages
described in #22176 there.

See #22717,.
This commit is contained in:
Vadim Zeitlin 2022-08-29 15:46:28 +02:00
parent 82cb8aa859
commit 8af645ed22
6 changed files with 65 additions and 1 deletions

View file

@ -64,6 +64,13 @@ public:
// GLogLevelFlags enum.
static void GTKSuppressDiagnostics(int flags = -1);
// Allow wxWidgets to control GTK diagnostics. This is recommended because
// it prevents spurious GTK messages from appearing, but can't be done by
// default because it would result in a fatal error if the application
// calls g_log_set_writer_func() itself.
static void GTKAllowDiagnosticsControl();
// implementation only from now on
// -------------------------------

View file

@ -33,7 +33,14 @@ public:
m_next = NULL;
}
// Function to call to install this filter as the active one.
// Allow installing our own log writer function, we don't do it by default
// because this results in a fatal error if the application had already
// called g_log_set_writer_func() on its own.
static void Allow() { ms_allowed = true; }
// Function to call to install this filter as the active one if we're
// allowed to do this, i.e. if Allow() had been called before.
//
// Does nothing and just returns false if run-time glib version is too old.
bool Install();
@ -56,6 +63,10 @@ private:
gsize n_fields,
gpointer user_data);
// False initially, indicating that we're not allowed to install our own
// logging function.
static bool ms_allowed;
// False initially, set to true when we install wx_log_writer() as the log
// writer. Once we do it, we never change it any more.
static bool ms_installed;

View file

@ -1061,6 +1061,25 @@ public:
*/
static void GTKSuppressDiagnostics(int flags = -1);
/**
Allows wxWidgets to selectively suppress some GTK messages.
This function can be called to allow wxWidgets to control GTK message
logging. You must @e not call it if your application calls the @c
g_log_set_writer_func() function itself, as this function can be only
called once.
It is recommended to call this function in your overridden version of
wxApp::OnInit() to allow wxWidgets to suppress some spurious GTK error
messages, e.g. the ones that happen whenever wxNotebook pages are
removed with the current GTK versions.
@onlyfor{wxgtk}
@since 3.2.1
*/
static void GTKAllowDiagnosticsControl();
///@}
/**

View file

@ -145,6 +145,10 @@ public:
/**
Destroys the wxNotebook object.
@note When using wxGTK, destroying notebook can result in spurious GTK
diagnostic messages, you may use wxApp::GTKAllowDiagnosticsControl() to
suppress them.
*/
virtual ~wxNotebook();

View file

@ -37,6 +37,13 @@ bool MyApp::OnInit()
if ( !wxApp::OnInit() )
return false;
#ifdef __WXGTK__
// Many version of wxGTK generate spurious diagnostic messages when
// destroying wxNotebook (or removing pages from it), allow wxWidgets to
// suppress them.
GTKAllowDiagnosticsControl();
#endif // __WXGTK__
#if wxUSE_HELP
wxHelpProvider::Set( new wxSimpleHelpProvider );
#endif

View file

@ -184,6 +184,7 @@ bool wxApp::DoIdle()
namespace wxGTKImpl
{
bool LogFilter::ms_allowed = false;
bool LogFilter::ms_installed = false;
LogFilter* LogFilter::ms_first = NULL;
@ -205,6 +206,9 @@ LogFilter::wx_log_writer(GLogLevelFlags log_level,
bool LogFilter::Install()
{
if ( !ms_allowed )
return false;
if ( !ms_installed )
{
if ( glib_check_version(2, 50, 0) != 0 )
@ -278,12 +282,24 @@ void wxApp::GTKSuppressDiagnostics(int flags)
s_logFilter.SetLevelToIgnore(flags);
s_logFilter.Install();
}
/* static */
void wxApp::GTKAllowDiagnosticsControl()
{
wxGTKImpl::LogFilter::Allow();
}
#else // !wxHAS_GLIB_LOG_WRITER
/* static */
void wxApp::GTKSuppressDiagnostics(int WXUNUSED(flags))
{
// We can't do anything here.
}
/* static */
void wxApp::GTKAllowDiagnosticsControl()
{
// And don't need to do anything here.
}
#endif // wxHAS_GLIB_LOG_WRITER/!wxHAS_GLIB_LOG_WRITER
//-----------------------------------------------------------------------------