kamyu104 / LintCode

📝 C++11 Solutions of All 289 LintCode Problems (No More Updates)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

word-search-ii Memory leak

eudiwffe opened this issue · comments

The code for problem 132 : word search ii has memory leak
https://github.com/kamyu104/LintCode/blob/master/C++/word-search-ii.cpp
In line 14 : p->leaves[c] = new TrieNode;
the struct TrieNode has no user define constructor, but use c++ default constructor.
In line 28~34, implement user define destructor ~TrieNode() to override c++ default destructor.
However, this ~TrieNode() just recur itself, not delete TrieNode, bring about memory leak.
A simple modification is:
In line 28 : void clean(){
In line 31 : delete kv.second;
In line 57 : trie.clean();
That means: use c++ default destructor and manually call func clean() to free memory.

commented

Thank you for your suggestion. But I've tested the dtor of the original TrieNode class in an infinite loop. It not only does user-defined dtor but also default dtor to free the memory. So, I believe there is no memory leak in the original one. You can try it! :)