vector :: push_back(A *)创建泄漏?

时间:2015-06-17 18:41:29

标签: c++ vector push-back

我正在尝试创建一个非常简单的虚拟文件系统(表示为树)(对于我的FTP服务器)映射到我真实的几个地方。节点由Node对象表示,该对象在向量中保存指向其内容的指针。

我知道对于每一个新的都有一个删除正确的对象(调试到cout),但我从valgrind得到一些“肯定丢失”的字节。

我不仅在对象中持有指针,而且还在Filesystem类中保存指针,我在Filesystem的析构函数中删除它们,因此Node中保存的指针发生的事情应该无关紧要......对吗?或者我在这里遗漏了什么?

的valgrind:

==23638== 8 bytes in 1 blocks are definitely lost in loss record 1 of 1
==23638==    at 0x4C2B0E0: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==23638==    by 0x407786: __gnu_cxx::new_allocator<Node*>::allocate(unsigned long, void const*) (new_allocator.h:104)
==23638==    by 0x40733F: std::_Vector_base<Node*, std::allocator<Node*> >::_M_allocate(unsigned long) (stl_vector.h:168)
==23638==    by 0x406FEB: _ZNSt6vectorIP4NodeSaIS1_EE19_M_emplace_back_auxIJS1_EEEvDpOT_ (stl_vector.h:404)
==23638==    by 0x406F2F: _ZNSt6vectorIP4NodeSaIS1_EE12emplace_backIJS1_EEEvDpOT_ (vector.tcc:101)
==23638==    by 0x40404F: std::vector<Node*, std::allocator<Node*> >::push_back(Node*&&) (stl_vector.h:920)
==23638==    by 0x402FD1: Directory::addDirectoryNode(Directory*) (Directory.cpp:20)
==23638==    by 0x409600: Filesystem::addDirectory(std::string, std::string) (Filesystem.cpp:47)
==23638==    by 0x40B519: Configuration::loadPairs(std::basic_ifstream<char, std::char_traits<char> >&) (Configuration.cpp:85)
==23638==    by 0x40B85C: Configuration::load(std::string) (Configuration.cpp:103)
==23638==    by 0x4081A9: main (main.cpp:41)

我不太确定还有什么用处。

Filesystem.hpp:

class Filesystem
{
    public:
        Filesystem ( );
        ~Filesystem ( );
        void addDirectory ( string name, string path ); // the first string is the name of the dir in the virtual FS, path is where the directory actually is. 
        void addFile ( string );
    private:
    Directory * root;
    Directory * pwd;
    map <string, Node *> nodes;
};

Filesystem::~Filesystem ( )
{
    for ( auto & it : nodes )
    {
        if ( it.second != NULL )
        {
            cout << "deleting " << it . second -> getName ( ) << endl;
            delete it.second;
        }
    }
}

class Directory : public Node
{
    public:
        Directory ( string name, string path, Directory * mum ) : Node ( name, path ) { parent = mum; }; //assigns name and path, should be clear
        void addDirectoryNode ( Directory * );
    private:
    Directory * parent = NULL;
    vector <Node *> contents;
};

void Filesystem::addDirectory ( string name, string path ) 
{
    Directory * toAdd = new Directory ( name, path + "/", NULL );
    cout << "created " << name << ", path: " << toAdd -> getPath ( ) << endl;
    map <string, Node * > newEntries = toAdd -> loadDirectoryContents ( toAdd -> getDirectoryContents ( ) );
    nodes . insert ( pair <string, Node * > ( toAdd -> getPath ( ), toAdd ) );
    nodes . insert ( newEntries . begin ( ), newEntries . end ( ) );

    if ( name == "/" ) // I know that the first directory added will be "/"
        root = pwd = toAdd;

    else
        root -> addDirectoryNode ( toAdd );
}

// reads the content of this directory and returns it in a set, directories will have a "/" appended
set<string> Directory::getDirectoryContents (  ) const
{
    DIR * dir;
    dirent * currNode;
    set<string> names;

    dir = opendir ( path . c_str ( ) );

    while ( ( currNode = readdir ( dir ) ) )
    {
        if ( string ( currNode -> d_name ) == "." || string ( currNode -> d_name ) == ".." )
            continue;

        if ( currNode -> d_type == DT_DIR ) 
            names.insert ( string ( currNode-> d_name ) + "/" ) ;

        else if ( currNode -> d_type == DT_REG )
            names.insert ( string ( currNode -> d_name ) ) ;

    }

    closedir ( dir );
    return names;
}

// recursively loads the real contents of this directory into the virtual one
map <string, Node * > Directory::loadDirectoryContents ( set <string> names )
{
    map <string, Node * > newEntries;
    for ( auto & it : names )
    {
        if ( it [ ( it . length ( ) - 1 ) ], "/" )
        {
            Directory * newDir = new Directory ( it, path + it, this );
            map <string, Node * > newSubEntries = newDir -> loadDirectoryContents ( newDir -> getDirectoryContents ( ) );
            contents . push_back ( newDir );
            newEntries . insert ( pair <string, Node * > ( path + it, newDir ) );
            newEntries . insert ( newSubEntries . begin ( ), newSubEntries . end ( ) );
        }
        else
        {
            File * newFile;
            contents . push_back ( newFile = new File ( path + it, path ) ); 
            newEntries . insert ( pair <string, Node * > ( it, newFile ) ); 
        }
    }

    return newEntries;
}

void Directory::addDirectoryNode ( Directory * directory )
{
    contents.push_back ( directory );
}

1 个答案:

答案 0 :(得分:1)

如果Node类中没有虚析构函数,则通过基类指针删除Directory对象是未定义的行为。不会调用Directory类的析构函数,也不会调用包含向量的析构函数。这可能会导致内存泄漏。