链接列表矢量中的内存泄漏

时间:2017-04-22 17:17:12

标签: 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                                                                                                                                  
}

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;
}

私有变量是:

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;
    };
};

0 个答案:

没有答案