Always create wxStaticBitmap in the widgets sample

Not creating it when the default image wasn't found resulted in a crash,
so always create wxStaticBitmap using a fallback bitmap if necessary.

Also add a wxCHECK_RET to prevent the crash from happening in a similar
situation in the future.
This commit is contained in:
Vadim Zeitlin
2018-09-22 14:33:29 +02:00
parent 1cf981e243
commit 455a45f5a8
2 changed files with 26 additions and 11 deletions

View File

@@ -35,6 +35,7 @@
#include "wx/textctrl.h" #include "wx/textctrl.h"
#endif #endif
#include "wx/artprov.h"
#include "wx/filename.h" #include "wx/filename.h"
#include "wx/generic/statbmpg.h" #include "wx/generic/statbmpg.h"
@@ -126,28 +127,39 @@ void StatBmpWidgetsPage::RecreateWidget()
{ {
wxDELETE(m_statbmp); wxDELETE(m_statbmp);
wxString filepath = m_filepicker->GetPath(); wxBitmap bmp;
if ( filepath.empty() )
return;
wxImage image(filepath); wxString filepath = m_filepicker->GetPath();
if (! image.IsOk() ) if ( !filepath.empty() )
{ {
wxLogMessage("Reading image from file '%s' failed.", filepath.c_str()); wxImage image(filepath);
return; if ( image.IsOk() )
{
bmp = image;
}
else
{
wxLogMessage("Reading image from file '%s' failed.", filepath.c_str());
}
}
if ( !bmp.IsOk() )
{
// Show at least something.
bmp = wxArtProvider::GetBitmap(wxART_MISSING_IMAGE);
} }
long style = GetAttrs().m_defaultFlags; long style = GetAttrs().m_defaultFlags;
if (m_radio->GetSelection() == 0) if (m_radio->GetSelection() == 0)
{ {
m_statbmp = new wxStaticBitmap(this, wxID_ANY, wxBitmap(image), m_statbmp = new wxStaticBitmap(this, wxID_ANY, bmp,
wxDefaultPosition, wxDefaultSize, wxDefaultPosition, wxDefaultSize,
style); style);
} }
else else
{ {
m_statbmp = new wxGenericStaticBitmap(this, wxID_ANY, wxBitmap(image), m_statbmp = new wxGenericStaticBitmap(this, wxID_ANY, bmp,
wxDefaultPosition, wxDefaultSize, wxDefaultPosition, wxDefaultSize,
style); style);
} }

View File

@@ -680,8 +680,11 @@ void WidgetsFrame::ConnectToWidgetEvents()
it != widgets.end(); it != widgets.end();
++it ) ++it )
{ {
(*it)->Bind(wxEVT_SET_FOCUS, &WidgetsFrame::OnWidgetFocus, this); wxWindow* const w = *it;
(*it)->Bind(wxEVT_KILL_FOCUS, &WidgetsFrame::OnWidgetFocus, this); wxCHECK_RET(w, "NULL widget");
w->Bind(wxEVT_SET_FOCUS, &WidgetsFrame::OnWidgetFocus, this);
w->Bind(wxEVT_KILL_FOCUS, &WidgetsFrame::OnWidgetFocus, this);
} }
} }