title: Cannot Destroy Dynamically created Menu Item in Delphi
body: <p>Firstly, yes I have looked all over the net and still cannot seem to destroy dynamically created menu items.  Using Delphi XE. I create the items thus (for the purposes of the exercise SubMenuName is 'Test1':</p>
  
  <pre><code>MenuItemCreated := TMenuItem.Create(PopupMenu1);
  MenuItemCreated.Caption:= SubMenuCaption
  MenuItemCreated.Hint := SubMenuHint;
  MenuItemCreated.Name := SubMenuName;
  MenuItemCreated.OnClick :=  SubMenuClick;
  MenuItemCreated.AutoHotkeys := maManual;
  MySubMenu.Add(MenuItemCreated);
  </code></pre>
  
  <p>There is no issue using the sub-menu(s) created. The procedure SubMenuClick works as it should, and I identify the correct subMenu item so no issues there. What I then do is an application logout which is supposed to free the dynamically created sub-menus using this code (although I have tried many variations):</p>
  
  <pre><code>  // Get rid of the menu items created
    While MySubMenu.Count &gt; 0 do
    begin
      Itemtodelete := MySubMenu.Items[0];
      FreeandNil(ItemtoDelete);
    end;
  </code></pre>
  
  <p>I have put in showmessage() debug lines that show the component names of the menu items being freeandnil'd and they are what I'd expect, ie. 'Test1' and any others I've created. I then log back in to my application (which was still running, but with me logged out).  The software then tries to recreate the same sub menus with the same names (as nothing has changed as far as my application is concerned and they were previously disposed of (supposedly)).  I immediately get the exception raised:</p>
  
  <p>Error: A component Named Test1 already exists</p>
  
  <p>I am at a complete loss as to how to dispose of the submenu items so that I can recreate them later with the same names.</p>
  
  <p>Any help greatly appreciated.</p>
  
  <p>Thanks,
  KB</p>