How to modulate your application?

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
matthewflagg
Earned a small fee
Earned a small fee
Posts: 17
Joined: Mon Jul 10, 2017 3:23 pm

How to modulate your application?

Post by matthewflagg »

Is there a proper or preferred way to modulate your application?
What I mean by this is there a way to separate your MenuBar code from your wxFrame custom class. I would like to have a custom MenuBar class that holds all my wxMenuBar code and just add it to my custom wxFrame class. I just don't like having everything in MyFrame class.

I understand there is probably a dozen different ways to do this, How do you guys prefer to do this?

When I tried doing this I created a memory leak.

CODE:
Main.cpp

Code: Select all

#include <wx/wx.h>

#include "MainFrame.h"

class MyApp : public wxApp
{
public:
	virtual bool OnInit() wxOVERRIDE;
};

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit()
{
	MainFrame *frame = new MainFrame(NULL);

	frame->Show(TRUE);

	SetTopWindow(frame);

	return TRUE;
}
MainFrame.h

Code: Select all

#pragma once

#include <wx/artprov.h>
#include <wx/xrc/xmlres.h>
#include <wx/menu.h>
#include <wx/toolbar.h>
#include <wx/string.h>
#include <wx/stattext.h>
#include <wx/gdicmn.h>
#include <wx/font.h>
#include <wx/colour.h>
#include <wx/settings.h>
#include <wx/sizer.h>
#include <wx/textctrl.h>
#include <wx/frame.h>

#include "MainMenu.h"

class MainFrame : public wxFrame
{

public:

	MainFrame(wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = wxT("Test Program"), 
		const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize(500, 497), 
		long style = wxDEFAULT_FRAME_STYLE | wxTAB_TRAVERSAL);

	~MainFrame();

	void MenuExit(wxCommandEvent& event);

protected:
	MainMenu* mainmenu;

	wxStaticText* Text_Title;

private:

};
MainFrame.cpp

Code: Select all

#include "MainFrame.h"

MainFrame::MainFrame(wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style) 
: wxFrame(parent, id, title, pos, size, style)
{
	this->SetSizeHints(wxDefaultSize, wxDefaultSize);

	//Layout
	wxBoxSizer* MainSizer;
	MainSizer = new wxBoxSizer(wxVERTICAL);

	//Menu
	mainmenu = new MainMenu(0);
	this->SetMenuBar(mainmenu->GetMenuBar());

	//Frame objects
	Text_Title = new wxStaticText(this, wxID_ANY, wxT("This is a long title name for this porogram"), 
		wxDefaultPosition, wxDefaultSize, 0);
	Text_Title->Wrap(-1);
	MainSizer->Add(Text_Title, 0, wxALIGN_CENTER_HORIZONTAL, 5);

	//Set objects to layout
	this->SetSizer(MainSizer);
	this->Layout();

	this->Centre(wxBOTH);

	//Events
	Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::MenuExit));
	Connect(wxID_EXIT, wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler(MainFrame::MenuExit));
}

MainFrame::~MainFrame()
{
}

void MainFrame::MenuExit(wxCommandEvent& event)
{
	Close(true);
}
MainMenu.h

Code: Select all

#pragma once

#include <wx/menu.h>


class MainMenu : public wxMenuBar
{
public:
	MainMenu(long style);
	~MainMenu();

	wxMenuBar* GetMenuBar();

protected:
	wxMenuBar* menu;
	wxMenu* m_file;
	wxMenu* m_about;

private:

};

inline MainMenu::MainMenu(long style)
{
	menu = new wxMenuBar(style);
	m_file = new wxMenu();
	wxMenuItem* mi_exit;
	mi_exit = new wxMenuItem(m_file, wxID_EXIT, wxString("Exit"), wxEmptyString, wxITEM_NORMAL);
	m_file->Append(mi_exit);

	menu->Append(m_file, wxT("File"));

}

inline MainMenu::~MainMenu()
{
	delete m_file;
	delete m_about;
	delete menu;
	delete this;
}

inline wxMenuBar* MainMenu::GetMenuBar()
{
	return menu;
}
When I run this in VS2015 It detects memory leacks and starts dumping objects after I close the application. What am I doing wrong? Point me in the right direction?

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

Re: How to modulate your application?

Post by doublemax »

Code: Select all

menu = new wxMenuBar(style);
This doesn't make sense. MainMenu derives from wxMenuBar, so it *is* a wxMenuBar, you don't need to create another one. But you must call the base constructor.
Use the source, Luke!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7479
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How to modulate your application?

Post by ONEEYEMAN »

Just fix memory leaks.

Thank you.
matthewflagg
Earned a small fee
Earned a small fee
Posts: 17
Joined: Mon Jul 10, 2017 3:23 pm

Re: How to modulate your application?

Post by matthewflagg »

doublemax wrote:

Code: Select all

menu = new wxMenuBar(style);
This doesn't make sense. MainMenu derives from wxMenuBar, so it *is* a wxMenuBar, you don't need to create another one. But you must call the base constructor.
Yup your right not sure if I was thinking when I did this.
ONEEYEMAN wrote: Just fix memory leaks.
I got it working.

Thanks you guys!
Post Reply