Image

Imageruakh wrote in Imagecpp

Memory leakage in C++ class

The other day, I received an anonymous comment to a really old entry of mine here. The following is approximately the comment; I've had to reconstruct what he must have meant to post, since he seemed to format his post for text e-mail rather than for HTML-parsed comment (I don't think he's very familiar with LJ). My comments are in [bracketed, signed, italicized text - Ran].



Memory leakage in C++ class
Before I begin, I must first make the following disclaimer: Although I have considerable programming experience, I do not consider myself by any means to be an expert C++ programmer. The following may be nothing more than a relection of my ignorance. If what I describe is not an actual bug, I would be very appreciative if you could briefly explain to me how I can de-allocate memory allocated by a set class, since everything I have tried is in vain and every computer scientist I have asked seems as dumbfounded as I.


I am running g++ 2.96 on a i386 redhat linux platform. [I'm running g++ 3.2 on WinXP, using Cygwin, and MSVC++ 6. - Ran] I think I discovered a bug. I compiled and ran the following program.

#include <set>

int main()
{
     unsigned long j;
     set <long>*o = new set <long>();
     for(j = 1; j <= 1000000; j++)
     {
         (*o).insert(j);
     }
     (*o).clear();
     delete o;
     while(1); //[Intentional infinite loop. - Ran]
}

[I had to insert a using std::set; statement to get this program to compile; I think that's a known difference between g++ 2.x and g++ 3.x. - Ran]

Using top, I monitored memory usage and noticed the delete operation did not free the 24 MB allocated by the multiple calls to insert in the for loop. [On g++ 3.2: using Windows Task Manager, I find the program to require 25,300 KB once in the infinite loop, compared to 1,784 KB when I comment-out everything in int main() except the infinite loop. On MSVC++ 6: the program took huge amounts of memory at first, but eventually settled down to 868 KB; when I commented out everything except the infinite loop, the program took 688 KB. - Ran] This problem seems confined to the set and map classes. No matter what I seem to do, I cannot de-allocate memory allocated by the set or map classes. I do not enounter the problem with the vector class. For example, I did not observe using top any memory leakage when I compiled and ran

#include <vector>

int main()
{
     unsigned long j;
     vector <long>*o = new vector <long>();
     for(j = 1; j <= 10000000; j++)
     {
         (*o).push_back(j);
     }
     (*o).clear();
     delete o;
     while(1);
}

[Again, I had to add a using std::vector; statement for this to compile. Using g++ 3.2: this program took 1,916 KB, compared to the same 1,784 KB when I commented-out everything except the infinite loop. Using MSVC++ 6: this program took huge amounts of memory at first, but eventually settled to 820 KB, compared to the same 688 KB when I commented-out everything except the infinite loop. - Ran]

I know that clear alone at least for a vector does not de-allocate memory since it merely erases the elements without altering the capacity. Nevertheless, shouldn't the delete operation, whether it is applied to an empty vector, set, or map, always perform the necessary de-allocation?



So, to summarize: in g++ 2.96 and g++ 3.2, the destructor for std::set <long> - or possibly its insert member function - doesn't seem to de-allocate all the memory it should, unlike the destructor for std::vector <long>. In MSVC++ 6, both destructors seem to de-allocate all the memory properly.

Does anyone know anything about this?

P.S. The original comment may be found here, if anyone is interested.