The following function appears in the OctoMap code:
class AbstractOcTreeNode {};
-> They declare an empty class
AbstractOcTreeNode** children;
-> This is declared in the OcTreeDataNode
class header file
template <typename T>
void OcTreeDataNode<T>::allocChildren() {
children = new AbstractOcTreeNode*[8];
for (unsigned int i=0; i<8; i++) {
children[i] = NULL;
}
}
Wouldn't this cause memory leak? Shouldn't it be:
template <typename T>
void OcTreeDataNode<T>::allocChildren() {
children = new AbstractOcTreeNode*[8];
for (unsigned int i=0; i<8; i++) {
delete children[i];
children[i] = NULL;
}
}
What am I missing? Thanks for the help!
You'd want to delete the entire array, not each of the individual array elements
You must always match a
new
with adelete
, and anew[]
with adelete[]
, without mixing them.For completeness (I'm guessing at the context) since the name of the function is
allocChildren
I assume it is their intention tonew[]
the array and not cleanup the memory, yet. Hopefully there would be a matchingdeallocChildren
that woulddelete[]
this memory later.