*** Leaked objects detected: 1 instance(s) of class CCustomMenuBarModel


#1

Hi, everyone:

I get a problem when i setMenuBar(new CCustomMenuBarModel()) in the class MainWindow.

I create a project and define a custom class “menubarModel” , override three virtual functions :getMenuBarNames, getMenuForIndex, menuItemSelected.

when I click the button “close”, it accur error : “*** Leaked objects detected: 1 instance(s) of class CCustomMenuBarModel” jassertfalse.

I have no ideal ,anyone can help me ?


#2

Hi,

You’re leaking memory because you pass a “new” CCustomMenuBarModel object to the setMenuBar() method but it never gets deleted. Try storing your custom MenuBarModel object in a ScopedPointer in your MainWindow and then pass that to the setMenuBar() method.

Ed


#3

Thanks for your replay, I fix my code as youu said, the memory leak problem has solved.

however ,error accur ,you can see the image:

when I setMenuBar(nullptr) in the ~MainWindow(),the exe was correct.

now i was puzzled…[quote=“ed95, post:2, topic:20433, full:true”]
Hi,

You’re leaking memory because you pass a “new” CCustomMenuBarModel object to the setMenuBar() method but it never gets deleted. Try storing your custom MenuBarModel object in a ScopedPointer in your MainWindow and then pass that to the setMenuBar() method.

Ed
[/quote]


#4

You can still do both, the ScopedPointer keeps ownership and destroys the instance.
But something in the background seems to reference the menubarmodel, so if you keep the setMenuBar(nullptr); in the ~MainWindow() you should be fine… You can also define the destruction sequence by calling

~MainWindow() {
    setMenuBar (nullptr);    // removes the reference to the model
    menubarModel = nullptr;  // calls implicitly delete on the ScopedPointers target instance
}

#5

Thanks , you said is right and i try the method ,the question is solved.:slight_smile: