[gparted] Fix memory leak of FileSystem objects in init_filesystems() (#749036)



commit 40820bada7b672377450705891308883b5e0bc6d
Author: Mike Fleetwood <mike fleetwood googlemail com>
Date:   Fri Apr 24 21:38:34 2015 +0100

    Fix memory leak of FileSystem objects in init_filesystems() (#749036)
    
    The FileSystem objects stored in the FILESYSTEM_MAP are allocated once
    using new in init_filesystems() but never deleted.
    
    Valgrind output fragment:
    
        # valgrind --leak-check=full ./gparted
        ==29314== 353 (72 direct, 281 indirect) bytes in 1 blocks are definitely lost in loss record 6,287 of 
6,905
        ==29314==    at 0x4A075FC: operator new(unsigned long) (vg_replace_malloc.c:298)
    >>  ==29314==    by 0x46EDA5: GParted::GParted_Core::init_filesystems() (GParted_Core.cc:106)
        ==29314==    by 0x46EC5F: GParted::GParted_Core::GParted_Core() (GParted_Core.cc:96)
        ==29314==    by 0x4A74F4: GParted::Win_GParted::Win_GParted(std::vector<Glib::ustring, 
std::allocator<Glib::ustring> > const&) (Win_GParted.cc:51)
        ==29314==    by 0x4D600A: main (main.cc:56)
        ...
        ==29314== 161 (72 direct, 89 indirect) bytes in 1 blocks are definitely lost in loss record 6,119 of 
6,905
        ==29314==    at 0x4A075FC: operator new(unsigned long) (vg_replace_malloc.c:298)
    >>  ==29314==    by 0x46F50C: GParted::GParted_Core::init_filesystems() (GParted_Core.cc:124)
        ==29314==    by 0x46EC5F: GParted::GParted_Core::GParted_Core() (GParted_Core.cc:96)
        ==29314==    by 0x4A74F4: GParted::Win_GParted::Win_GParted(std::vector<Glib::ustring, 
std::allocator<Glib::ustring> > const&) (Win_GParted.cc:51)
        ==29314==    by 0x4D600A: main (main.cc:56)
    
    GParted_Core.cc source:
    
       102  void GParted_Core::init_filesystems()
       103  {
       104          FILESYSTEM_MAP[ FS_UNKNOWN ]         = NULL ;
       105          FILESYSTEM_MAP[ FS_CLEARED ]         = NULL ;
    >> 106          FILESYSTEM_MAP[ FS_BTRFS ]           = new btrfs() ;
       ...
    >> 124          FILESYSTEM_MAP[ FS_XFS ]             = new xfs() ;
       125          FILESYSTEM_MAP[ FS_BITLOCKER ]       = NULL ;
    
    Fix by deleting all FILESYSTEM_MAP pointers.  Note that delete on a NULL
    pointer is defined by C++ as a safe do nothing operation.
    
        C++ FAQ / Do I need to check for null before delete p?
        https://isocpp.org/wiki/faq/freestore-mgmt#delete-handles-null
    
    Fixing this reduces the valgrind reported definitely lost memory blocks
    count from 25 down to 6.  19 FileSystem objects deleted and 19 memory
    blocks no longer lost.
    
    Bug 749036 - FileSystem objects are memory leaked in init_filesystems()

 include/GParted_Core.h |    1 +
 src/GParted_Core.cc    |   12 ++++++++++++
 2 files changed, 13 insertions(+), 0 deletions(-)
---
diff --git a/include/GParted_Core.h b/include/GParted_Core.h
index f96b0f9..538c4cf 100644
--- a/include/GParted_Core.h
+++ b/include/GParted_Core.h
@@ -36,6 +36,7 @@ public:
        ~GParted_Core() ;
 
        void init_filesystems() ;
+       static void fini_filesystems();
        void find_supported_filesystems() ;
        void set_user_devices( const std::vector<Glib::ustring> & user_devices ) ;
        void set_devices( std::vector<Device> & devices ) ;
diff --git a/src/GParted_Core.cc b/src/GParted_Core.cc
index cf45fab..8f8bbf6 100644
--- a/src/GParted_Core.cc
+++ b/src/GParted_Core.cc
@@ -128,6 +128,16 @@ void GParted_Core::init_filesystems()
        FILESYSTEM_MAP[ FS_LINUX_SWSUSPEND ] = NULL ;
 }
 
+void GParted_Core::fini_filesystems()
+{
+       std::map<FILESYSTEM, FileSystem *>::iterator fs_iter;
+       for ( fs_iter = FILESYSTEM_MAP.begin() ; fs_iter != FILESYSTEM_MAP.end() ; fs_iter ++ )
+       {
+               delete fs_iter->second;
+               fs_iter->second = NULL;
+       }
+}
+
 void GParted_Core::find_supported_filesystems()
 {
        std::map< FILESYSTEM, FileSystem * >::iterator f ;
@@ -4026,6 +4036,8 @@ PedExceptionOption GParted_Core::ped_exception_handler( PedException * e )
 
 GParted_Core::~GParted_Core() 
 {
+       // Delete file system map entries
+       fini_filesystems();
 }
 
 Glib::Thread *GParted_Core::mainthread;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]