VS2022 reporting wxMenu memory leak Topic is solved

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
rkamarowski
Knows some wx things
Knows some wx things
Posts: 34
Joined: Fri Jan 14, 2022 5:54 pm

VS2022 reporting wxMenu memory leak

Post by rkamarowski »

Windows 11
Visual Studio 2022
C++
wxWidgets 3.2.0

I have the following code in my wxFrame:

Code: Select all

wxMenu* fileMenu = new wxMenu();
If I close the app by clicking the 'X' in the upper right hand corner of the window VS2022 is reporting memory leaks. If I close app using the red Stop Debugging button there are no memory leaks reported. If I comment out the above line of code no leaks are reported either way the app is closed.

Am I having memory leaks, and why?
Thank you,
Bob K.
User avatar
doublemax
Moderator
Moderator
Posts: 19161
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: VS2022 reporting wxMenu memory leak

Post by doublemax »

That's not enough code. If the menu is added to a wxMenuBar, and that wxMenuBar is attached to the wxFrame, there should be no memory leak.

Check the "minimal" sample that comes with the wxWidgets source code for reference.
Use the source, Luke!
rkamarowski
Knows some wx things
Knows some wx things
Posts: 34
Joined: Fri Jan 14, 2022 5:54 pm

Re: VS2022 reporting wxMenu memory leak

Post by rkamarowski »

Code: Select all

#include "PMainFrame.h"

using namespace pWidgets;


PMainFrame::PMainFrame()
	: wxFrame(NULL, wxID_ANY, "EES Photography", wxPoint(10,10))
{
	OnInit();
}

void PMainFrame::OnInit()
{
	Bind(wxEVT_MENU, &PMainFrame::OnAbout, this, TEXT_ABOUT);
	Bind(wxEVT_MENU, &PMainFrame::OnExit, this, TEXT_EXIT);

	SetMinSize(wxSize(800, 600));
	SetSize(100, 100, 900, 700);
	Center(wxBOTH);

	wxMenu* fileMenu = new wxMenu();
	fileMenu->AppendSeparator();
	fileMenu->Append(TEXT_EXIT, wxT("E&xit"), wxT("Exit application"));

	wxMenu* routineMenu = new wxMenu();
	routineMenu->Append(ROUTINE_EDIT, wxT("&Edit routine"),
		wxT("Edit routine."));
	routineMenu->Append(ROUTINE_EXECUTE, wxT("&Run routine"),
		wxT("Run routine."));

	wxMenu* aboutMenu = new wxMenu;
	aboutMenu->Append(TEXT_ABOUT, wxT("&About"));

	wxMenuBar* menuBar = new wxMenuBar(wxMB_DOCKABLE);
	menuBar->Append(fileMenu, wxT("&File"));
	menuBar->Append(aboutMenu, wxT("&About"));

	SetMenuBar(menuBar);
}

void PMainFrame::OnExit(wxCommandEvent& event)
{
	Close(true);
}
User avatar
doublemax
Moderator
Moderator
Posts: 19161
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: VS2022 reporting wxMenu memory leak

Post by doublemax »

You're not adding routineMenu to the menubar.
Use the source, Luke!
rkamarowski
Knows some wx things
Knows some wx things
Posts: 34
Joined: Fri Jan 14, 2022 5:54 pm

Re: VS2022 reporting wxMenu memory leak

Post by rkamarowski »

Ugh... As always, thank you doublemax. Hopefully next time it will be something more interesting :)
Bob K.
Post Reply