Fix CmdFeatureMenuBuilder::MenuItem memory leak in RiuContextMenuLauncher

Address sanitizer detected a memory leak in CmdFeatureMenuBuilder::MenuItem
objects allocated during context menu creation.

Problem: The RiuContextMenuLauncher constructor was creating a temporary
CmdFeatureMenuBuilder object, populating it with menu items, then copying
the entire object to the member variable m_menuBuilder. This caused
unnecessary memory allocation and copying of std::vector<MenuItem> data.

Root cause: Inefficient constructor implementation:
  caf::CmdFeatureMenuBuilder menuBuilder;  // temp object
  for (cmd : commandIds) menuBuilder << cmd;  // populate temp
  m_menuBuilder = menuBuilder;  // copy entire vector (leak source)

Fix: Build the menu directly into the member variable instead of creating
a temporary object and copying it:
  for (cmd : commandIds) m_menuBuilder << cmd;  // direct construction

This eliminates the unnecessary temporary object creation and copy operation
that was causing the MenuItem vector allocation detected by the sanitizer.
The fix maintains identical functionality while being more efficient.
This commit is contained in:
Kristian Bendiksen committed 2025-09-04 09:04:30 +02:00
1 parent 89e940b1ea
commit aec17d54a7
1 file changed
+2 -5
@@ -42,15 +42,12 @@ RiuContextMenuLauncher::RiuContextMenuLauncher( QWidget* widget, const caf::CmdF
//--------------------------------------------------------------------------------------------------
RiuContextMenuLauncher::RiuContextMenuLauncher( QWidget* widget, const QStringList& commandIds )
{
caf::CmdFeatureMenuBuilder menuBuilder;
// Build menu directly into m_menuBuilder to avoid unnecessary copy
for ( const auto& cmd : commandIds )
{
menuBuilder << cmd;
m_menuBuilder << cmd;
}
m_menuBuilder = menuBuilder;
widget->installEventFilter( this );
}