// JP opened flex table

Click to See Complete Forum and Search --> : CCoolMenuManager - Memory Leak


Simon Clark
May 20th, 1998, 06:38 AM
I have found that the CCoolMenuManager class leaks memory in an application with a standard MFC MRU on the "File" menu. The "ConvertMenu" function continually allocates memory at the "pmd = new CMyItemData;" statement for the MRU items in the list everytime the menu drops down.


My guess is that when building the "File" menu, MFC dynamically removes the MRU items (causing the leak situation) before adding them to the list again.


A really quick fix is to put all of the code in the "if (bShowButtons) {" block statement inside another "if" statement like:-


if (info.wID = ID_FILE_MRU_FILE1 + _AFX_MRU_MAX_COUNT)

{

...

}


This cures the leak but fails to align the MRU list with the indented menu items in the rest of the "File" menu. The results are visually OK but if anyone else cares to suggest a better solution that aligns the text as well, then please do.

Joe Ouimet
July 30th, 1998, 04:22 PM
This same leak exists when you use an accelerator key. If anyone knows of an updated version, I would love to see it.


Joe

Michael Dye
January 27th, 1999, 05:22 AM
I've just found the same problem you reported months ago, and fixed it a different way:


LRESULT CCoolMenuManager::WindowProc(UINT msg, WPARAM wp, LPARAM lp)

{

... code removed ...


case WM_INITMENUPOPUP:

// Very important: must let frame window handle it first!

// Because if someone calls CCmdUI::SetText, MFC will change item to

// MFT_STRING, so I must change back to MFT_OWNERDRAW.

// ALSO CALL CONVERTMENU TO UNDO ANY PREVIOUS CONVERSION FIRST

ConvertMenu(CMenu::FromHandle((HMENU)wp), 0, FALSE, FALSE);

// THIS WILL STOP MEMORY LEAKS CAUSED BY MFC CALLING SetText

CSubclassWnd::WindowProc(msg, wp, lp);

OnInitMenuPopup(CMenu::FromHandle((HMENU)wp), (UINT)LOWORD(lp),

(BOOL)HIWORD(lp));

return 0;


... plenty more code removed ...


By calling convert menu to destroy stuff (as is done when the menu is closed) any allocated memory is removed. Means

no memory leakage but the down side is menus are regenerated all the time. Doesn't seem to have any negative in terms

of display though.

//JP added flex table