diff --git a/src/msw/menuitem.cpp b/src/msw/menuitem.cpp index 1704892306..65ffdb6a16 100644 --- a/src/msw/menuitem.cpp +++ b/src/msw/menuitem.cpp @@ -780,8 +780,11 @@ void wxMenuItem::DoSetBitmap(const wxBitmap& bmp, bool bChecked) { wxCHECK_RET( item == this, wxS("Non unique menu item ID?") ); - m_parentMenu->Remove(this); - m_parentMenu->Insert(pos, this); + // Use a copied value of m_parentMenu because it is + // nullified by Remove. + wxMenu *menu = m_parentMenu; + menu->Remove(this); + menu->Insert(pos, this); } //else: the item hasn't been inserted into the parent menu yet } diff --git a/tests/menu/menu.cpp b/tests/menu/menu.cpp index 0ae436f27f..efc2f0a170 100644 --- a/tests/menu/menu.cpp +++ b/tests/menu/menu.cpp @@ -420,6 +420,12 @@ void MenuTestCase::ChangeBitmap() // again etc... item->SetBitmap( wxBitmap(1, 1) ); + + // Force owner drawn usage by having a bitmap that's wider than the + // standard size. This results in rearranging the parent menu which + // hasn't always worked properly and lead to a null pointer exception. + item->SetBitmap( wxBitmap(512, 1) ); + wxDELETE(menu); }