From ce71b80ed9ad226a4aa45f14893480a0b3c6265b Mon Sep 17 00:00:00 2001 From: Alexander Fedorov Date: Tue, 22 Jul 2025 18:52:42 +0300 Subject: [PATCH] Resolve "SWT Resource was not properly disposed" for "Statistics" * implement dispose for `TmfPieChartViewer` * use `dispose` properly --- .../PieChartViewerStateNoContentSelected.java | 11 ++---- .../viewers/piecharts/TmfPieChartViewer.java | 35 ++++++++++++++----- .../statistics/TmfStatisticsViewer.java | 5 +-- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/viewers/piecharts/PieChartViewerStateNoContentSelected.java b/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/viewers/piecharts/PieChartViewerStateNoContentSelected.java index cdfc113507..203080102a 100644 --- a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/viewers/piecharts/PieChartViewerStateNoContentSelected.java +++ b/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/viewers/piecharts/PieChartViewerStateNoContentSelected.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2015 Ericsson + * Copyright (c) 2015, 2025 Ericsson and others * * All rights reserved. This program and the accompanying materials are * made available under the terms of the Eclipse Public License 2.0 which @@ -10,6 +10,7 @@ * * Contributors: * Alexis Cabana-Loriaux - Initial API and implementation + * Alexander Fedorov (ArSysOp) - fix "SWT Resource was not properly disposed" * *******************************************************************************/ @@ -43,13 +44,7 @@ public PieChartViewerStateNoContentSelected(final TmfPieChartViewer context) { synchronized (context) { if (!context.isDisposed()) { // Have to get rid of the time-range PieChart - if (context.getTimeRangePC() != null) { - if (!context.getTimeRangePC().isDisposed()) { - context.getTimeRangePC().dispose(); - } - context.setTimeRangePC(null); - } - + context.disposeTimeRangePC(); context.updateGlobalPieChart(); // update the global chart so it takes all the place context.getGlobalPC().getLegend().setPosition(SWT.RIGHT); diff --git a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/viewers/piecharts/TmfPieChartViewer.java b/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/viewers/piecharts/TmfPieChartViewer.java index 6cbbfaa5ab..fbfe63f40e 100644 --- a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/viewers/piecharts/TmfPieChartViewer.java +++ b/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/viewers/piecharts/TmfPieChartViewer.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2015, 2020 Ericsson + * Copyright (c) 2015, 2025 Ericsson and others * * All rights reserved. This program and the accompanying materials are * made available under the terms of the Eclipse Public License 2.0 which @@ -10,6 +10,7 @@ * * Contributors: * Alexis Cabana-Loriaux - Initial API and implementation + * Alexander Fedorov (ArSysOp) - fix "SWT Resource was not properly disposed" * *******************************************************************************/ @@ -442,17 +443,11 @@ public synchronized void reinitializeCharts() { if (isDisposed()) { return; } - - if (getGlobalPC() != null && !getGlobalPC().isDisposed()) { - getGlobalPC().dispose(); - } + disposeGlobalPC(); fGlobalPC = new TmfPieChart(this, SWT.NONE); getGlobalPC().getTitle().setText(fGlobalPCname); getGlobalPC().getAxisSet().getXAxis(0).getTitle().setText(""); //Hide the title over the legend //$NON-NLS-1$ - if (getTimeRangePC() != null && !getTimeRangePC().isDisposed()) { - getTimeRangePC().dispose(); - fTimeRangePC = null; - } + disposeTimeRangePC(); layout(); setCurrentState(new PieChartViewerStateNoContentSelected(this)); } @@ -582,4 +577,26 @@ public void select(String id) { pieChart.redraw(); } } + + @Override + public void dispose() { + disposeGlobalPC(); + disposeTimeRangePC(); + super.dispose(); + } + + void disposeGlobalPC() { + if (fGlobalPC != null && !fGlobalPC.isDisposed()) { + fGlobalPC.dispose(); + fGlobalPC = null; + } + } + + void disposeTimeRangePC() { + if (fTimeRangePC != null && !fTimeRangePC.isDisposed()) { + fTimeRangePC.dispose(); + fTimeRangePC = null; + } + } + } diff --git a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/viewers/statistics/TmfStatisticsViewer.java b/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/viewers/statistics/TmfStatisticsViewer.java index f9d79808a1..609ee151de 100644 --- a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/viewers/statistics/TmfStatisticsViewer.java +++ b/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/internal/tmf/ui/viewers/statistics/TmfStatisticsViewer.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2012, 2020 Ericsson + * Copyright (c) 2012, 2025 Ericsson and others * * All rights reserved. This program and the accompanying materials are * made available under the terms of the Eclipse Public License 2.0 which @@ -13,6 +13,7 @@ * Alexandre Montplaisir - Port to ITmfStatistics provider * Patrick Tasse - Support selection range * Bernd Hufmann - Fix range selection updates + * Alexander Fedorov (ArSysOp) - fix "SWT Resource was not properly disposed" *******************************************************************************/ package org.eclipse.tracecompass.internal.tmf.ui.viewers.statistics; @@ -186,7 +187,7 @@ private void internalDispose() { // Clean the model for this viewer TmfStatisticsTreeManager.removeStatTreeRoot(getTreeID()); - fPieChartViewer.reinitializeCharts(); + fPieChartViewer.dispose(); } // ------------------------------------------------------------------------