menubar is null which crashes program

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Shadowblitz16
Earned a small fee
Earned a small fee
Posts: 14
Joined: Sun Oct 13, 2019 2:32 am

menubar is null which crashes program

Post by Shadowblitz16 » Sun Oct 13, 2019 8:10 pm

I am getting a error when trying to do mdi via inheritance.
It keeps saying something about the menu being NULL?
Image

Code: Select all

#pragma once
#include <wx/wxprec.h>
#include "ChildWindow.h"

class MainWindow : public wxMDIParentFrame
{
	public:
		//Constructor and Deconstructor
		 MainWindow(const wxString& title);
		~MainWindow();

		ChildWindow* childWindow;
		wxMenuBar*   menuBar;
		wxMenu*      menu1;

};

Code: Select all

#include "MainWindow.h"
#include <wx/artprov.h>



MainWindow::MainWindow(const wxString& title) : wxMDIParentFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250, 150))
{

	childWindow = new ChildWindow(this, "hi");
	menuBar     = new wxMenuBar();
	menu1       = new wxMenu();


	//wxWidgets default wxMenuItem example
	menu1->Append(wxID_ANY, _("&New\tCtrl+N"  ));
	menu1->Append(wxID_ANY, _("&Open\tCtrl+O" ));
	menu1->Append(wxID_ANY, _("&Save\tCtrl+S" ));
	menu1->Append(wxID_ANY, _("&Quit\tCtrl+Q" ));

	menuBar->Append(menu1, _("&File" ));
	SetMenuBar(menuBar);
}


MainWindow::~MainWindow()
{
}

Code: Select all

#pragma once
#include <wx/wxprec.h>


class ChildWindow : public wxMDIChildFrame
{
	public:
		ChildWindow(wxMDIParentFrame* parent, wxString title);
	   ~ChildWindow();

	//wxWidgets
	wxButton* buttonAdd;
	wxButton* buttonSub;
	wxStaticText* label;

	//Events
	void ButtonAddClicked(wxCommandEvent& event);
	void ButtonSubClicked(wxCommandEvent& event);

	private:
	int count;
};

Code: Select all

#include "ChildWindow.h"


ChildWindow::ChildWindow(wxMDIParentFrame* parent, wxString title) : wxMDIChildFrame(parent, wxID_ANY, title)
{
	count = 0;

	SetBackgroundColour(wxSystemSettings::GetColour(wxSystemColour::wxSYS_COLOUR_WINDOW));
	buttonSub = new wxButton    (this, wxID_ANY, "-", wxPoint(24, 24), wxSize(24, 24));
	buttonAdd = new wxButton    (this, wxID_ANY, "+", wxPoint(48, 24), wxSize(24, 24));
	label     = new wxStaticText(this, wxID_ANY, "0", wxPoint(72, 24), wxSize(24, 96));

	buttonAdd->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &ChildWindow::ButtonAddClicked, this);
	buttonSub->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &ChildWindow::ButtonSubClicked, this);
}

ChildWindow::~ChildWindow()
{

}

void ChildWindow::ButtonAddClicked(wxCommandEvent& WXUNUSED(event))
{
	count++;
	label->SetLabel(wxString::Format(wxT("%d"), count));
}

void ChildWindow::ButtonSubClicked(wxCommandEvent& WXUNUSED(event))
{
	count--;
	label->SetLabel(wxString::Format(wxT("%d"), count));
}

Code: Select all

'WxWidgets.exe' (Win32): Loaded 'C:\Users\shadowblitz16\source\repos\WxWidgets\Debug\WxWidgets.exe'. Symbols loaded.
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Users\shadowblitz16\source\repos\WxWidgets\Debug\wxbase312ud_vc_custom.dll'. Symbols loaded.
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\user32.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\gdi32.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\lpk.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\usp10.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\advapi32.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\shell32.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\shlwapi.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ole32.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\version.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp140d.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\vcruntime140d.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbased.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\api-ms-win-core-localization-l1-2-0.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\api-ms-win-core-processthreads-l1-1-1.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\api-ms-win-core-file-l1-2-0.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\api-ms-win-core-timezone-l1-1-0.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\api-ms-win-core-file-l2-1-0.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\api-ms-win-core-synch-l1-2-0.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Users\shadowblitz16\source\repos\WxWidgets\Debug\wxmsw312ud_core_vc_custom.dll'. Symbols loaded.
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\comdlg32.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\winsxs\x86_microsoft.windows.common-controls_6595b64144ccf1df_6.0.7601.24483_none_2b200f664577e14b\comctl32.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\winspool.drv'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\oleaut32.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\winmm.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\oleacc.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\uxtheme.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msimg32.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\imm32.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msctf.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\dwmapi.dll'. 
c:\wxwidgets-3.1.2\src\common\menucmn.cpp(725): assert "menubar" failed in wxMenuBase::Attach(): menu can't be attached to NULL menubar
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\dbghelp.dll'. 
DBGHELP: Symbol Search Path: .
DBGHELP: SymSrv load failure: symsrv.dll
DBGHELP: .\wxbase312ud_vc_custom.pdb - file not found
DBGHELP: .\dll\wxbase312ud_vc_custom.pdb - file not found
DBGHELP: .\symbols\dll\wxbase312ud_vc_custom.pdb - file not found
DBGHELP: wxbase312ud_vc_custom - private symbols & lines 
         C:\wxWidgets-3.1.2\lib\vc_dll\wxbase312ud_vc_custom.pdb
DBGHELP: .\wxmsw312ud_core_vc_custom.pdb - file not found
DBGHELP: .\dll\wxmsw312ud_core_vc_custom.pdb - file not found
DBGHELP: .\symbols\dll\wxmsw312ud_core_vc_custom.pdb - file not found
DBGHELP: wxmsw312ud_core_vc_custom - private symbols & lines 
         C:\wxWidgets-3.1.2\lib\vc_dll\wxmsw312ud_core_vc_custom.pdb
DBGHELP: .\WxWidgets.pdb - file not found
DBGHELP: .\exe\WxWidgets.pdb - file not found
DBGHELP: .\symbols\exe\WxWidgets.pdb - file not found
DBGHELP: WxWidgets - public symbols & lines 
         C:\Users\shadowblitz16\source\repos\WxWidgets\Debug\WxWidgets.pdb
dbghelp: SymFromAddr() failed: Attempt to access invalid address.
dbghelp: SymFromAddr() failed: Attempt to access invalid address.
dbghelp: SymFromAddr() failed: Attempt to access invalid address.
dbghelp: SymFromAddr() failed: Attempt to access invalid address.
dbghelp: SymFromAddr() failed: Attempt to access invalid address.
dbghelp: SymFromAddr() failed: Attempt to access invalid address.
dbghelp: SymFromAddr() failed: Attempt to access invalid address.
DBGHELP: .\wkernel32.pdb - file not found
DBGHELP: .\dll\wkernel32.pdb - file not found
DBGHELP: .\symbols\dll\wkernel32.pdb - file not found
DBGHELP: wkernel32.pdb - file not found
DBGHELP: kernel32 - export symbols
dbghelp: SymFromAddr() failed: Attempt to access invalid address.
DBGHELP: .\wntdll.pdb - file not found
DBGHELP: .\dll\wntdll.pdb - file not found
DBGHELP: .\symbols\dll\wntdll.pdb - file not found
DBGHELP: wntdll.pdb - file not found
DBGHELP: ntdll - export symbols
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\duser.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\xmllite.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ole32.dll'. 
'WxWidgets.exe' (Win32): Unloaded 'C:\Windows\SysWOW64\ole32.dll'
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\clbcatq.dll'. 
'WxWidgets.exe' (Win32): Loaded 'C:\Windows\SysWOW64\shell32.dll'. 
'WxWidgets.exe' (Win32): Unloaded 'C:\Windows\SysWOW64\shell32.dll'
'WxWidgets.exe' (Win32): Unloaded 'C:\Windows\SysWOW64\xmllite.dll'
The thread 0x1a4c has exited with code 0 (0x0).
WxWidgets.exe has triggered a breakpoint.

The program '[5364] WxWidgets.exe' has exited with code 0 (0x0).

User avatar
doublemax
Moderator
Moderator
Posts: 15289
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: menubar is null which crashes program

Post by doublemax » Sun Oct 13, 2019 8:26 pm

Just a guess: try moving the childWindow = new ChildWindow(this, "hi"); at the end of the ctor, after SetMenuBar()
Use the source, Luke!

alys666
Super wx Problem Solver
Super wx Problem Solver
Posts: 318
Joined: Tue Oct 18, 2016 2:31 pm

Re: menubar is null which crashes program

Post by alys666 » Sun Oct 13, 2019 8:30 pm

in my code i m doing all additions to menubar after call of SetMenuBar(..).
ubuntu 16.04, wxWidgets 3.0.4

Shadowblitz16
Earned a small fee
Earned a small fee
Posts: 14
Joined: Sun Oct 13, 2019 2:32 am

Re: menubar is null which crashes program

Post by Shadowblitz16 » Sun Oct 13, 2019 9:51 pm

oh weird.
is this a bug? I don't really understand why it needs to be after.

catalin
Moderator
Moderator
Posts: 1594
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: menubar is null which crashes program

Post by catalin » Mon Oct 14, 2019 4:08 am

Shadowblitz16 wrote:
Sun Oct 13, 2019 9:51 pm
is this a bug?
It's most likely a documentation issue. All samples attach menu bar after adding menus to it, and SetMenuBar() docs do say "Under Windows, [...] be sure to initialize data members properly before calling SetMenuBar()" which is probably referring to exactly this issue, although in a rather poor way.

User avatar
doublemax
Moderator
Moderator
Posts: 15289
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: menubar is null which crashes program

Post by doublemax » Mon Oct 14, 2019 5:02 am

Shadowblitz16 wrote:
Sun Oct 13, 2019 9:51 pm
oh weird.
is this a bug? I don't really understand why it needs to be after.
Does this mean it works when you move the code?

BTW: This is not a normal "crash", this is an assert message from wxWidgets telling you that you did something wrong.
All samples attach menu bar after adding menus to it, and SetMenuBar() docs do say "Under Windows, [...] be sure to initialize data members properly before calling SetMenuBar()"
I think the problem here is the construction of the childframe, because the frame will try to add that child to its own menubar which has to exist at that point.
Use the source, Luke!

alys666
Super wx Problem Solver
Super wx Problem Solver
Posts: 318
Joined: Tue Oct 18, 2016 2:31 pm

Re: menubar is null which crashes program

Post by alys666 » Mon Oct 14, 2019 8:25 am

imo doublemax is right. seems creation of mdi window needs properly set toolbar of parent.

i just have set toolbar before additions, because all my menues are dynamic, and i created empty tool bar, registered it at parent window, and later added needed submenues by context sensitive functions.
ubuntu 16.04, wxWidgets 3.0.4

Shadowblitz16
Earned a small fee
Earned a small fee
Posts: 14
Joined: Sun Oct 13, 2019 2:32 am

Re: menubar is null which crashes program

Post by Shadowblitz16 » Tue Oct 15, 2019 6:41 pm

@doublemax yes moving it made it work.

Post Reply