I have TreeView with context menu (QMenu) in my program. But it have to be dynamic one, I mean than menu items created in runtime. Each item have his own set of actions.
My problem is how to avoid memory leaks.

I will illustrate it by example:

Qt Code:
  1. //menu initialization:
  2. treeMenu = new QMenu(ui->objectTree);
  3. connect(ui->objectTree,SIGNAL(customContextMenuRequested(const QPoint&)),this,SLOT(treePopupShow(const QPoint&)));
  4.  
  5. // slot
  6. void MyProgram::treePopupShow(const QPoint & point) {
  7. QModelIndex index = ui->objectTree->indexAt(point);
  8. if(index.isValid())
  9. {
  10. TreeItem * item = static_cast<TreeItem *>(index.internalPointer());
  11. if(item)
  12. {
  13. treeMenu->clear();
  14. switch(item->getType())
  15. {
  16. case NodeType1:
  17. QMenu * submenu1 = treeMenu->addMenu(tr("submenu1")); // memory leak
  18. foreach(QString action,actionList1)
  19. {
  20. QAction * action = submenu1->addAction(action);
  21. action->setData(action);
  22. }
  23. break;
  24. case NodeType2:
  25. QMenu * submenu2 = treeMenu->addMenu(tr("submenu2")); // memory leak
  26. foreach(QString action,actionList2)
  27. {
  28. QAction * action = submenu2->addAction(action);
  29. action->setData(action);
  30. }
  31. break;
  32. }
  33. }
  34. }
To copy to clipboard, switch view to plain text mode 

It will be tens of such submenus like submenu1 and submenu2. and tens of top items. treeMenu->clear() clears only actions, not submenus. May be I have to store all of them to delete every time when menu is shown?