C++ 链表向量中的内存泄漏

C++ 链表向量中的内存泄漏,c++,vector,struct,memory-leaks,C++,Vector,Struct,Memory Leaks,我有两个外观非常相似的函数,但其中一个会造成内存泄漏,我不明白为什么 Hasher::~Hasher() { clearTable(table); //IF I CHOOSE THIS, I HAVE MEMORY LEAKS clearTable(); //IF I CHOOSE THIS, THERE ARE NO MEMORY LEAKS

我有两个外观非常相似的函数,但其中一个会造成内存泄漏,我不明白为什么

Hasher::~Hasher()
{
    clearTable(table); //IF I CHOOSE THIS, I HAVE MEMORY LEAKS
    clearTable();      //IF I CHOOSE THIS, THERE ARE NO MEMORY LEAKS                                                                                                                                  
}

void Hasher::clearTable()
{
    for (uint i = 0; i < size; ++i)
            if (table[i])
                    clearWord(table[i]);//You can assume this cleans the linked list

}
void Hasher::clearTable(vector<Word *> &t)
{
    for (uint i = 0; i < t.size(); ++i)
            if (t[i])
                    clearWord(t[i]);//You can assume this cleans the linked list
}

void clearWord(Word *word)
{        
    if (word->next)
            clearWord(word->next);
    delete word;
}

为什么不使用
std::list
std::shared_ptr
?但是有人会造成内存泄漏,我不明白为什么--它会造成内存泄漏,因为您承担了手动处理动态分配内存的任务,而在这样做的过程中,您正在犯错误。这是一个高层次的答案——如果你想要一个更详细的答案,让我们看看所有的代码,即a。或者你可以直接使用
std::list
,它可以正确地完成任务,并且放弃重新发明轮子的尝试
vector<Word *> table;
unsigned int size;    //  ==table.size()
struct Word{
    std::string name;
    std::vector<Entry> entries;
    Word *next;                                                                                                               
    Word(std::string a, Entry b) {
            name = a; next = NULL;
            entries.push_back(b);
    };
};
struct Entry{
std::string fullName;
    std::vector<Location> loc;
    Entry(std::string a, std::vector<Location> b){
            fullName = a;
            loc = b;
    };
    Entry(const Entry &a){
            fullName = a.fullName;
            loc.push_back(a.loc[0]);
    };
};
struct Location{
    uint file, line;
    DirNode * dir;
    Location(uint a, uint b, DirNode *c){
            file = a;
            line = b;
            dir  = c;
    };
    Location(const Location &a){
            file = a.file;
            line = a.line;
            dir  = a.dir;
    };
};