I’m creating a map grid (a 2D discrete number of points) using the concept of mapPixel, a class:
class MapPixel
{
friend class Map;
protected:
int x;
int y;
float height;
float vegetation;
std::vector<const MapPixel*> neib;
...methods declaration, default constructor/destructor
where neib is a list of pointers to other MapPixels, adjacent to then.
I’m using the method
void MapPixel::addNeib(const MapPixel* neib_)
{
neib.push_back(neib_);
}
to add a pointer to a neiber pixel to build the graph (since the borders have less neibs than the center pixels, this list is size dependent).
My procedure is to have a class Map with a member
MapPixel **pixels;
in the constructor Map::Map() using
pixels = new MapPixel*[width];
for (int i = 0; i < width; i++)
pixels[i] = new MapPixel[height];
I use the method MapPixel::addNode() to build the network (e.g.)
pixels[i][j].addNeib(&pixels[i][j+1]);
and in the Map::~Map() I delete MapPixel by the inverse order (without deleting the neibs to avoid double free):
for (int i = 0; i < width; i++)
delete pixels[i];
delete pixels;
Valgrind says there are several big memory leaks like this:
2,509,088 bytes in 39,205 blocks are possibly lost in loss record 4,071 of 4,071
in MapPixel::addNeib(MapPixel const*) in Source/mappixel.cpp:52
1: malloc in vg_replace_malloc.c:266
2: operator new(unsigned long) in /usr/lib/libstdc++.6.0.9.dylib
3: __gnu_cxx::new_allocator<MapPixel const*>::allocate(unsigned long, void const*) in ...
4: std::_Vector_base<MapPixel const*, std::allocator<MapPixel const*> >::_M_allocate(unsigned long) in stl_vector.h:131
5: std::vector<MapPixel const*, std::allocator<MapPixel const*> >::_M_insert_aux(__gnu_cxx::__normal_iterator<MapPixel const**, std::vector<MapPixel const*, std::allocator<MapPixel const*> > >, MapPixel const* const&) in vector.tcc:271
6: std::vector<MapPixel const*, std::allocator<MapPixel const*> >::push_back(MapPixel const* const&) in stl_vector.h:608
7: MapPixel::addNeib(MapPixel const*) in mappixel.cpp:52
all related to the line 52:
neib.push_back(neib_);
Does anyone understands this? Now I lost confidence on if I can use std::vector to build the neibs of my pixels.
Note that valgrind said “possibly lost”, not “definitely lost”. The difference is important. See here for the exact meanings.
That error is about blocks allocated by the
vector<>implementation code, most likely to resize the block of memory containing the elements as thevectorgrows. You might get these if you are allocating instances ofMapPixeland forgetting to free them, since the containingvectorwould then not be able to free its memory, but then you would also get errors about your own code.Unless! when you free the
pixelsarrays, are you usingdelete[]ordelete?Update: you’re using
delete. You need to usedelete[]. This is indeed a memory leak. Anything you allocate withnew[]must be freed withdelete[], otherwise the proper destructor (even one automatically generated by the compiler) will only be called for the first element.