From 89e940b1ea550bd1aba246ef09fc89dab5d6aa07 Mon Sep 17 00:00:00 2001 From: Kristian Bendiksen Date: Fri, 29 Aug 2025 13:29:57 +0000 Subject: [PATCH] Fix QProxyStyle memory leak in PdmUiTreeViewStyle creation Address sanitizer detected a memory leak in QProxyStyle objects created during GUI theme updates. Problem: The updateGuiTheme() function was called multiple times (e.g., during initialization) and repeatedly created new PdmUiTreeViewStyle objects for every PdmUiTreeViewWidget without checking if the widget already had the correct style set. Root cause: Each call to updateGuiTheme() would: 1. Iterate through ALL widgets in the application 2. Create a new PdmUiTreeViewStyle for every tree view widget 3. Call setStyle() which should take ownership, but repeated calls were causing memory leaks Fix: Added a check to only create and set a new PdmUiTreeViewStyle if the widget doesn't already have one. This prevents repeatedly creating new styles for the same widgets during multiple theme update calls. The fix uses dynamic_cast to check if the current style is already a PdmUiTreeViewStyle before creating a new one. --- ApplicationLibCode/UserInterface/RiuGuiTheme.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ApplicationLibCode/UserInterface/RiuGuiTheme.cpp b/ApplicationLibCode/UserInterface/RiuGuiTheme.cpp index 76bc1087a8..0c2b912b83 100644 --- a/ApplicationLibCode/UserInterface/RiuGuiTheme.cpp +++ b/ApplicationLibCode/UserInterface/RiuGuiTheme.cpp @@ -360,7 +360,12 @@ void RiuGuiTheme::updateGuiTheme( RiaDefines::ThemeEnum theme ) widget->style()->polish( widget ); if ( caf::PdmUiTreeViewWidget* treeViewWidget = dynamic_cast( widget ) ) { - treeViewWidget->setStyle( new caf::PdmUiTreeViewStyle ); + // Only set a new style if the widget doesn't already have a PdmUiTreeViewStyle + // to prevent memory leaks from repeatedly creating new styles + if ( !dynamic_cast( treeViewWidget->style() ) ) + { + treeViewWidget->setStyle( new caf::PdmUiTreeViewStyle ); + } } } }