我创建使用mapPixel方式的概念的图格(点的2D离散数),一类:内存泄漏(Valgrind的)
class MapPixel
{
friend class Map;
protected:
int x;
int y;
float height;
float vegetation;
std::vector<const MapPixel*> neib;
...methods declaration, default constructor/destructor
其中neib是列表指向与其相邻的其他MapPixels的指针。
我使用的方法
void MapPixel::addNeib(const MapPixel* neib_)
{
neib.push_back(neib_);
}
的指针添加到neiber像素构建的曲线图(因为边界具有比中心像素少neibs,该列表尺寸依赖)。
我的做法是有一类地图使用
pixels = new MapPixel*[width];
for (int i = 0; i < width; i++)
pixels[i] = new MapPixel[height];
我使用的方法MapPixel :: ADDNODE()在构造函数中地图成员
MapPixel **pixels;
::地图()建立网络(例如)
pixels[i][j].addNeib(&pixels[i][j+1]);
并在Map ::〜Map()中我按照相反的顺序删除MapPixel(不删除neibs避免双重免费):
for (int i = 0; i < width; i++)
delete pixels[i];
delete pixels;
Valgrind的说,有几个大的内存泄漏这样的:
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
所有与线52:
neib.push_back(neib_);
有谁明白这一点?现在我失去了信心,如果我可以使用std :: vector来构建像素的neibs。
在内存泄漏的可能出现的问题,你应该给我们俩如何页头(新),以及如何你的dealloc(删除)......这里部分缺失。例如。你以相反的顺序删除MaxPixel,好吧;但你是否是像素本身?我想答案是肯定的,而且给出的答案适用。尽管如此,让全新的/删除代码来看看会更好! – ShinTakezou
添加了删除代码。我说的不是因为这个,因为Valgrind并没有抱怨新像素/新像素[我],所以我认为这个问题不在那里。 –
应该不是'delete []'而是? – ShinTakezou