codemonkey codemonkey - 7 months ago 50
C++ Question

Data Leaking Occurs When Deleting a Node From BST

else if(parent->rightChildPtr==nodePtr)
delPtr = nodePtr;
nodePtr = nodePtr->leftChildPtr;

delPtr->leftChildPtr = NULL;
delete delPtr;

I have this if statement instruction inside my removeItem function. I don't know how data leaking occurs. As you see, I point the node to be deleted with another pointer,which is delPtr, then set nodeptr to it's right, and delete the delptr and it's contents. So the parent of this NodePtr should see "nodePtr's rightchildptr" as its child as well, but no.


You'd need to show more code but it seems very likely to me that while you are setting nodePtr, you aren't updating the parent node's appropriate child pointer. So it still holds the former value of nodePtr which is now invalid as it has been deleted.