Skip to content

Commit

Permalink
Merge pull request #28 from lxylxy123456/huffman
Browse files Browse the repository at this point in the history
Fix memory leak in src/Huffman{Main,Test}.cpp
  • Loading branch information
lxylxy123456 committed Dec 17, 2020
2 parents 5d05385 + 5b833b1 commit 7c0089d
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 1 deletion.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ VALGRIND := $(filter-out valgrind/RaceExampleTest,$(VALGRIND))
# VALGRIND_ALL has currently unavailable tests filtered out
VALGRIND_ALL := $(VALGRIND)
VALGRIND_ALL := $(filter-out valgrind/FibTest,$(VALGRIND_ALL))
VALGRIND_ALL := $(filter-out valgrind/HuffmanTest,$(VALGRIND_ALL))
VALGRIND_ALL := $(filter-out valgrind/IntervalTreeTest,$(VALGRIND_ALL))
VALGRIND_ALL := $(filter-out valgrind/MatVecTest,$(VALGRIND_ALL))
VALGRIND_ALL := $(filter-out valgrind/PSquareMatrixMultiplyTest,$(VALGRIND_ALL))
Expand Down
7 changes: 7 additions & 0 deletions include/Huffman.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ class CNode {
CNode(T d, size_t f): disp(d), freq(f), left(nullptr), right(nullptr) {}
CNode(T d, size_t f, CNode<T>* l, CNode<T>* r): disp(d), freq(f),
left(l), right(r) {}
CNode<T>* recursive_destruct() {
if (left)
delete left->recursive_destruct();
if (right)
delete right->recursive_destruct();
return this;
}
friend std::ostream& operator<<(std::ostream& o, const CNode<T>& r) {
return o << r.disp;
}
Expand Down
1 change: 1 addition & 0 deletions src/HuffmanMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@ int main(int argc, char *argv[]) {
// call function
CNode<char>* B = Huffman(C);
TreePrint(B, HuffmanNavigator<CNode<char>*>());
delete B->recursive_destruct();
return 0;
}
1 change: 1 addition & 0 deletions src/HuffmanTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ int test(size_t n) {
// call function
CNode<char>* B = Huffman(C);
TreePrint(B, HuffmanNavigator<CNode<char>*>());
delete B->recursive_destruct();
return 0;
}

Expand Down

0 comments on commit 7c0089d

Please sign in to comment.