Skip to content

Commit

Permalink
Set root=NULL in clear(), fixes #123.
Browse files Browse the repository at this point in the history
Add unit test for clear() in test_pruning.cpp
  • Loading branch information
ahornung committed Jan 13, 2017
1 parent dff6556 commit a0915f6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
3 changes: 2 additions & 1 deletion octomap/include/octomap/OcTreeBaseImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,8 @@ namespace octomap {
/// recursive call of writeData()
std::ostream& writeNodesRecurs(const NODE*, std::ostream &s) const;

/// recursive delete of node and all children (deallocates memory)
/// Recursively delete a node and all children. Deallocates memory
/// but does NOT set the node ptr to NULL nor updates tree size.
void deleteNodeRecurs(NODE* node);

/// recursive call of deleteNode()
Expand Down
7 changes: 4 additions & 3 deletions octomap/include/octomap/OcTreeBaseImpl.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ namespace octomap {
if (this->root){
deleteNodeRecurs(root);
this->tree_size = 0;
this->root = NULL;
// max extent of tree changed:
this->size_changed = true;
}
Expand Down Expand Up @@ -664,15 +665,15 @@ namespace octomap {

if (node->children != NULL) {
for (unsigned int i=0; i<8; i++) {
if (node->children[i] != NULL)
this->deleteNodeRecurs(static_cast<NODE*>(node->children[i]));
if (node->children[i] != NULL){
this->deleteNodeRecurs(static_cast<NODE*>(node->children[i]));
}
}
delete[] node->children;
node->children = NULL;
} // else: node has no children

delete node;
node = NULL;
}


Expand Down
23 changes: 23 additions & 0 deletions octomap/src/testing/test_pruning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ using namespace octomath;
int main(int argc, char** argv) {
float res = 0.01f;
OcTree tree(res);

EXPECT_EQ(tree.size(), 0);
tree.prune();
EXPECT_EQ(tree.size(), 0);

point3d singlePt(-0.05f, -0.02f, 1.0f);
OcTreeKey singleKey;
Expand Down Expand Up @@ -233,6 +237,25 @@ int main(int argc, char** argv) {


}

tree.write("pruning_test_out.ot");

{
std::cout << "\nClearing tree / recursive delete\n===============================\n";

OcTree emptyTree(0.1234);
EXPECT_EQ(emptyTree.size(), 0);
emptyTree.clear();
EXPECT_EQ(emptyTree.size(), emptyTree.calcNumNodes());
EXPECT_EQ(emptyTree.size(), 0);

tree.clear();
EXPECT_EQ(tree.size(), 0);
EXPECT_EQ(tree.size(), tree.calcNumNodes());

tree.prune();
EXPECT_EQ(tree.size(), 0);
}

tree.write("pruning_test_out.ot");
std::cerr << "Test successful.\n";
Expand Down

0 comments on commit a0915f6

Please sign in to comment.