Tracker: Fix memory leak

BMenuField's constructor (called on line 715) used to be passed
fDirMenu. The constructor would create a new BMenuItem using fDirMenu.
TFilePanel::Init did not want this menu so it removed it with a call to
RemoveItem (located on line 721). This call, however, does not actually
free the memory. It just removes it from the menu. The refactor that I
did here fixes this by never creating the new BMenuItem in the first
place. This is done by passing NULL into BMenuField's constructor
instead of fDirMenu. It happens that the refactoring also cleans up
TFilePanel::Init slightly.

Change-Id: I05ef24f429fb309ff41806e342d275f832772b5e
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6486
Reviewed-by: waddlesplash <waddlesplash@gmail.com>
This commit is contained in:
Zardshard 2023-05-31 13:00:37 -04:00 committed by waddlesplash
parent ef8e820bc7
commit 338fedd65a

View File

@ -699,8 +699,6 @@ TFilePanel::Init(const BMessage*)
fBackView->AddChild(fMenuBar);
// add directory menu and menufield
fDirMenu = new BDirMenu(0, this, kSwitchDirectory, "refs");
font_height ht;
be_plain_font->GetHeight(&ht);
const float f_height = ht.ascent + ht.descent + ht.leading;
@ -712,16 +710,14 @@ TFilePanel::Init(const BMessage*)
rect.right = rect.left + (spacing * 50);
rect.bottom = rect.top + (f_height > 22 ? f_height : 22);
fDirMenuField = new BMenuField(rect, "DirMenuField", "", fDirMenu);
fDirMenuField = new BMenuField(rect, "DirMenuField", "", NULL);
fDirMenuField->MenuBar()->SetFont(be_plain_font);
fDirMenuField->SetDivider(0);
fDirMenuField->MenuBar()->SetMaxContentWidth(rect.Width() - 26.0f);
// Make room for the icon
fDirMenuField->MenuBar()->RemoveItem((int32)0);
fDirMenu->SetMenuBar(fDirMenuField->MenuBar());
// the above is a weird call from BDirMenu
// ToDo: clean up
fDirMenu = new BDirMenu(fDirMenuField->MenuBar(),
this, kSwitchDirectory, "refs");
BEntry entry(TargetModel()->EntryRef());
if (entry.InitCheck() == B_OK)