Files
ResInsight/ApplicationLibCode/UserInterface/RiuContextMenuLauncher.cpp
T
Kristian Bendiksen 0cce2bb42d Fix critical RiuContextMenuLauncher memory leak by setting parent widget
Address sanitizer detected a memory leak in CmdFeatureMenuBuilder::MenuItem
objects caused by RiuContextMenuLauncher objects that were never deleted.

Problem: The RiuContextMenuLauncher class has two constructors:
1. RiuContextMenuLauncher(widget, CmdFeatureMenuBuilder&) - sets parent ✓
2. RiuContextMenuLauncher(widget, QStringList&) - missing parent ✗

The second constructor was missing the parent initialization, causing all
RiuContextMenuLauncher objects created with QStringList (used in 10+ locations)
to never be deleted, leading to memory leaks of their internal MenuItem vectors.

Root cause: Missing parent-child relationship meant Qt couldn't automatically
clean up these objects when their associated widgets were destroyed.

Fix: Added `: QObject( widget )` to the second constructor to match the first
constructor's behavior. This ensures Qt's automatic parent-child cleanup
handles the RiuContextMenuLauncher lifecycle properly.

This fixes the systemic issue affecting multiple plot types including:
- Correlation plots, Analysis plots, VFP plots, Well allocation plots
- All locations using `new RiuContextMenuLauncher(widget, {commands})`
2025-09-04 09:04:30 +02:00

79 lines
2.6 KiB
C++

/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2017 Statoil ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RiuContextMenuLauncher.h"
#include "RimContextCommandBuilder.h"
#include "cvfAssert.h"
#include <QContextMenuEvent>
#include <QEvent>
#include <QMenu>
#include <QWidget>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiuContextMenuLauncher::RiuContextMenuLauncher( QWidget* widget, const caf::CmdFeatureMenuBuilder& menuBuilder )
: QObject( widget )
, m_menuBuilder( menuBuilder )
{
widget->installEventFilter( this );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiuContextMenuLauncher::RiuContextMenuLauncher( QWidget* widget, const QStringList& commandIds )
: QObject( widget )
{
// Build menu directly into m_menuBuilder to avoid unnecessary copy
for ( const auto& cmd : commandIds )
{
m_menuBuilder << cmd;
}
widget->installEventFilter( this );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RiuContextMenuLauncher::eventFilter( QObject* watchedObject, QEvent* event )
{
if ( event->type() == QEvent::ContextMenu )
{
QMenu menu;
m_menuBuilder.appendToMenu( &menu );
if ( !menu.actions().empty() )
{
QContextMenuEvent* cme = static_cast<QContextMenuEvent*>( event );
CVF_ASSERT( cme );
menu.exec( cme->globalPos() );
}
return true;
}
// standard event processing
return QObject::eventFilter( watchedObject, event );
}