codemonkey codemonkey - 1 year ago 78
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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download