Welcome! Log In Create A New Profile

Advanced

Memory leak bug in bSearchTreeType class

Posted by robanaurochs 
Announcements Last Post
Announcement : Programming Students at UNISA School of Computing 06/19/2019 02:01PM
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
avatar Memory leak bug in bSearchTreeType class
July 10, 2006 08:50PM
Hi All

There's a memory leak bug in the Insert function of the bSearchTreeType template class defined in Malik on pp657-658.

The bug surfaces when you want to insert a duplicate value. If you follow the programme flow, the first thing the function does is to allocate memory for a new node.

If it finds that there already exists a node with the same value, it prints an error message and then exits the function. What happens to the newly allocated node? Well, the pointer to it is destroyed but not the node itself. It just hangs around in memory and can never be used again - without a pointer to tell you where it is, you can't access it. Hence the memory leak.

To fix the bug, just change the error checking part to:
if (current->info == insertItem)
{
  cerr<<"The insert item is already in the list - ";
  cerr<<"duplicates are not allowed."<<endl;

  delete newNode; // deallocate the memory

  return;
}
Sorry, only registered users may post in this forum.

Click here to login