Click to See Complete Forum and Search --> : Please Help !!! CMenu array problems


Hardeep Singh
April 29th, 1999, 06:17 AM
I'm having quite a lot of trouble with a program, which uses arrays of popup menus.

It creates global arrays of menus, and later when it needs them, it calls CreatePopupMenu() for as many CMenus as needed, in a loop. Here, It gives a Debug Assertion failure, since there's no check for the validity of the CMenu object. This problem is solved only when I test if the m_hMenu member is not NULL (I've tried IsMenu() and GetSafeHmenu()... both use this fundamental check as one of the conditions, and this one alone works well here).

But when I do this, I later get another Assertion Failure when I try to call AppendMenu with the same menu object. On debugging, I find that the particular object has m_hMenu equal to NULL.

Now what do I do? While creating, I have problems because the menu handle IS NOT NULL, and (if I check that) while appending, I have problems if it IS NULL. Is there a better, more reliable way of creating and filling up arrays of CMenus without these problems? This problem does not happen with the simple menu objects, only with the arrays.

Needless to say, when I build the app in Release mode, it runs, but crashes often. I can't give up arrays altogether, since I don't have time to rewrite the whole program, and I'm actually working on a prewritten program, just trying to fix it.

I mentioned that only as many menus are created as required, so the arrays may not be full, but this is not a simple problem of crossing the bounds or accessing some array element outside of the initialized range... I've checked that. the array indices are well within range and the loops begin and end properly.

Whatever the case, this is urgent and I'll really really be grateful if anyone can help me out.

Thanks,
-Hardeep.

N garg
May 1st, 1999, 09:21 PM
I have faced similar problems.
Send me a part of your code where you create and initialise the array .
Perhaps I would be able to help

Hardeep Singh
May 1st, 1999, 10:07 PM
Hey thanks, dude, but I got over that particular problem just by initializing the m_hMenu member to NULL just before calling CreatePopupMenu() for each array element.
But now there's another problem. I'm getting an assertion failure when I try to see any submenu. In the OnInitPopupMenu() function of my window (I haven't overridden it). The function calls GetSubMenu, which in turn calls FromHandle, and ::GetSubMenu in so e sequence. Just look at line 61 in Winmenu.cpp, and that's where I get an assertion failure. I can tell u that the submenus are stored in the arrays, and though the main menu is being created (It wasn't earlier), I can't see the submenus because of the assertion failure. Please take a look and tell me what I can do.

N garg
May 3rd, 1999, 09:10 AM
There is something wrong with your initialisation of the array. Send me that part of the code...

Hardeep Singh
May 3rd, 1999, 09:44 PM
// Here's where they are created (globally)
CMenu menu4[20],menu3[20],men,menu,menu0,menu1[60],menu2[60][100];

// Again, it's not my code, so please, don't curse me!

//this loop is for root level directories//
for(int k2=0;k2<l;k2++)
{
menu1[k2].m_hMenu = NULL;
menu1[k2].CreatePopupMenu();//create a popupmenu for each level0 directory
// Other code...
//this loop is for the subdirectories(level-1) of the level-0 directories

for(int l1=1;l1<d1;l1++)
{
CString j;
menu2[k2][l1].m_hMenu = NULL;
menu2[k2][l1].CreatePopupMenu();//create a popup for level-1 each dir
}
// other code...
// take a look at the appending code too, just in case...
if(filest.attrib&_A_SUBDIR)
{
menu2[k2][l1].AppendMenu(MF_POPUP,(UINT)h,filest.name);
}
else
{
menu2[k2][l1].AppendMenu(MF_STRING,++imenu1,filest.name);
}
// ....

menu1[k2].AppendMenu(MF_POPUP,(UINT)h0,dir2_name[k2][l1]);
// Appending in the level1 menu
{
h2[l1]=menu2[k2][l1].GetSafeHmenu();
if(l1==27||l1==54)
menu1[k2].AppendMenu(MF_POPUP|MF_MENUBARBREAK,(UINT)h2[l1],dir2_name[k2][l1]);
else
menu1[k2].AppendMenu(MF_POPUP,(UINT)h2[l1],dir2_name[k2][l1]);
}
// still more menus !!
menu4[uu1].m_hMenu = NULL;
menu4[uu1].CreatePopupMenu();
}




I've only sent fragments here, but otherwise, let me tell you that all the loops work properly. As you say, the problem may be with the way they're initialized, though I think you should take a look at the assignment part.
See if you can make sense of the above
-Hardeep.

Jason Teagle
May 4th, 1999, 02:56 AM
When you call AppendMenu(), are you sure that the handles you pass (HMENU - h and h0 in your code fragment) are valid handles to menus that ALREADY EXIST? The popup menu you append MUST already have been created before you can append it to another menu.

I would be interested in the whole of that (menu manipulation) code, as there are some pieces missing which may be important. I assume from the code that you are trying to build a menu structure that mirrors a directory structure?

E-mail the code to jteagle@solartron.com and I'll have a look.

Hardeep Singh
May 4th, 1999, 07:05 AM
Well, Jason, the guy who wrote this code was indeed trying to mirror a directory structure. I know one thing-the menu handles are DEFINITELY assigned, and properly too-because the program runs in release mode, and I can navigate through the entire directory structure properly. Any drive or directory is properly explored. But it crashes often in release mode, and to try to find out the reason, I'm trying to build it in debug mode.
Actually, the program behaves very erratically. It'll run fine for a few tries and then start to crash consistently and without fail, every time it's run. I still can't figure out the cause of this behaviour, but I figured that if I can get it to run in debug mode, it has to run in release mode without crashing... doesn't it?
Most of the times it crashes, I get an invalid page fault error. But once in a while I also get a gpf!
Does this give any clue?

Jason Teagle
May 4th, 1999, 07:25 AM
Much as I would like to say 'yes, it helps', I'm afraid I have to say 'not really'. The invalid page fault sounds like there may be an illegal memory access, but the 'runs for a while and then dies' sounds like a resource / memory leak problem - are these menus being built once only, or every time the directory changes / whenever you go to access the top-level menu?

I really could do with seeing the code, just the bit that builds / destroys / accesses the menus, if I am to be of any help to you. If you're worried about copyright / trade secrets, don't worry - I have no need to represent a directory as a menu, and I can have a *_REALLY_* bad memory when I need to.

jteagle@solartron.com