Bad Karma on menu

I’m trying to have an automatic sub menu to let the user access to a big number of files (over 2000)

So here is the code that gives me a crash ! There may be something which is not deleted, or, well I don’t know !

It happens if I close the plugin window while the menu is still showing…

[code]const int MOVIE_MENU_MAX_ITEM=40; // this is the max number of item inside each sub menus
PopupMenu m;

if (nb_Of_Sub_Menus==0)
{

(this part runs fine)

}
if (nb_Of_Sub_Menus>0)
{

PopupMenu* dialogMenu=new PopupMenu[nb_Of_Sub_Menus];

DirectoryIterator dir_iterator(File(OI->Movie_Path),false,T("*.kmv"),File::findFiles);
for (int sub=0;sub<nb_Of_Sub_Menus;sub++)
{
	int count=1;
	String s;
	s.empty;
	while((dir_iterator.next())&&(count++<=MOVIE_MENU_MAX_ITEM))
	{
		(dialogMenu[sub]).addItem (count,dir_iterator.getFile().getFileNameWithoutExtension());
	}

	s<<T("Movies ")<<((sub*MOVIE_MENU_MAX_ITEM)+1)<<T(" to ")<<(((sub+1)*MOVIE_MENU_MAX_ITEM));

	m.addSubMenu (s, dialogMenu[sub]);
}

const int rc = m.show();

if (rc)
{
for (int sub=0;sub<nb_Of_Sub_Menus;sub++)
{
(dialogMenu[sub]).clear(); // I tried this to avoid the crash
}
return (rc-1);
}
else
{
for (int sub=0;sub<nb_Of_Sub_Menus;sub++)
{
(dialogMenu[sub]).clear();
}
return -1;
}

[/code]

Well you should delete all those menu objects that you’re leaking, but where/what exactly is the crash?

Actually I tried ‘delete’ like this :

if (rc) { for (int sub=0;sub<nb_Of_Sub_Menus;sub++) { (dialogMenu[sub]).clear(); } delete dialogMenu; return (rc-1); } else { for (int sub=0;sub<nb_Of_Sub_Menus;sub++) { (dialogMenu[sub]).clear(); } delete dialogMenu; return -1; }

but it produced a bad delete… Maybe the syntax I use is wrong ?

Anyway if I don’t put this delete, I have a big crash when I close the plugin GUI that made the popup menu. I don’t have this crash when there are no sub menus.

(eventually I reach the ‘bad karma’ point)

Nicolas

Yes, your syntax looks a bit screwy to me. You actually don’t need to use new/delete at all - menus can just be created on the stack, as they get copied when you add them as a submenu. Probably you’re leaving some leaked objects floating around that are staying modal until later on when you try to close the app or something.

Indeed, that was it !

Thanks a lot Jules…

Nicolas