From 93e5d9fa15488a96d30252b17d279c286b915209 Mon Sep 17 00:00:00 2001 From: John Scipione Date: Fri, 13 Mar 2015 16:14:35 -0400 Subject: [PATCH] DiskUsage: Fix CID 1288122 In hrev48870 I made some updates to DiskUsage which accidentally caused this CID. Both the volume and item pointers were going out of scope without being deleted in the error case leading to a resource leak. This commit seeks to fix the problem by creating these objects as late as possible after the error checking. tempVolume, which, as it's name implies, is created temporarily on the stack is used instead of volume up until the point that AddTab() requires a more permanent heap-stored volume pointer. Same goes for the VolumeView and VolumeTab. name is created temporarily on the stack as well which works because it is copied when passed into VolumeView constructor by the grandparent BHandler before going out of scope. --- src/apps/diskusage/ControlsView.cpp | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/apps/diskusage/ControlsView.cpp b/src/apps/diskusage/ControlsView.cpp index 47840e3e09..67c1c84d00 100644 --- a/src/apps/diskusage/ControlsView.cpp +++ b/src/apps/diskusage/ControlsView.cpp @@ -224,20 +224,23 @@ ControlsView::VolumeTabView::AttachedToWindow() BVolume tempVolume; while (fVolumeRoster->GetNextVolume(&tempVolume) == B_OK) { - if (tempVolume.IsPersistent()) { - BVolume* volume = new BVolume(tempVolume); - VolumeTab* item = new VolumeTab(volume); - char name[B_PATH_NAME_LENGTH]; - if (volume->GetName(name) != B_OK) - continue; + if (!tempVolume.IsPersistent()) + continue; - if (strcmp(name, "system") == 0 - || strcmp(name, "config") == 0) { - // Don't include virtual volumes. - continue; - } - AddTab(new VolumeView(name, volume), item); + char name[B_PATH_NAME_LENGTH]; + if (tempVolume.GetName(name) != B_OK) + continue; + + if (strcmp(name, "system") == 0 + || strcmp(name, "config") == 0) { + // Don't include virtual volumes. + continue; } + + BVolume* volume = new BVolume(tempVolume); + VolumeView* volumeView = new VolumeView(name, volume); + VolumeTab* volumeTab = new VolumeTab(volume); + AddTab(volumeView, volumeTab); } // Begin watching mount and unmount events.