C++ 点网格上的内存泄漏(Valgrind)

C++ 点网格上的内存泄漏(Valgrind),c++,memory-leaks,valgrind,C++,Memory Leaks,Valgrind,我正在使用mapPixel的概念创建一个地图网格(一个二维离散点数),一个类: class MapPixel { friend class Map; protected: int x; int y; float height; float vegetation; std::vector<const MapPixel*> neib; ...methods declaration, default constructor/destruct

我正在使用mapPixel的概念创建一个地图网格(一个二维离散点数),一个类:

class MapPixel
{
    friend class Map;
protected:
    int x;
    int y;
    float height;
    float vegetation;

    std::vector<const MapPixel*> neib;
...methods declaration, default constructor/destructor

添加指向neiber像素的指针以构建图形(由于边框的neibs小于中心像素,因此此列表取决于大小)

我的过程是用一个成员创建一个类映射

MapPixel **pixels;
在构造函数Map::Map()中,使用

在地图中::~Map()我按相反顺序删除MapPixel(不删除NEIB以避免双重空闲):

所有与第52行相关的内容:

neib.push_back(neib_);
有人明白这一点吗?现在我对是否可以使用std::vector构建像素的NEIB失去了信心。

注意,valgrind说“可能丢失”,而不是“肯定丢失”。区别很重要。有关确切含义,请参见

该错误与
向量
实现代码分配的块有关,随着
向量
的增长,最有可能调整包含元素的内存块的大小。如果您分配了
MapPixel
的实例,而忘记释放它们,则可能会得到这些实例,因为包含
vector
的实例将无法释放其内存,但您也会收到有关自己代码的错误

除非!释放
像素
阵列时,是否使用
delete[]
delete


更新:您正在使用
delete
。您需要使用
delete[]
。这确实是内存泄漏。使用
new[]
分配的任何内容都必须使用
delete[]
释放,否则只会为第一个元素调用适当的析构函数(即使是由编译器自动生成的析构函数)。

正如前面提到的另一个答案,内存泄漏很可能是由错误的
delete
运算符引起的。在构造函数中,使用以下命令创建数组数组:

对于您的邻居,我不会使用std::vector,而是一个普通的
mapixel*neib[8](假设摩尔邻域)或者更确切地说
std::array neib。但我不知道你们对这件商品还有什么其他要求


除了内存管理,使用STL容器还可以带来其他好处,例如方便的成员函数,它们不会衰减为指针,仅举几个例子。

在可能出现的内存泄漏问题中,您应该告诉我们如何分配(新)和如何解除分配(删除)。。。这里少了一部分。例如,按相反顺序删除MaxPixel,ok;但是你知道像素本身吗?我想答案是肯定的,并且给出的答案适用。不过,最好有完整的new/delete代码来查看!添加了删除代码。我不是说是因为ValgRink不抱怨新的像素/新的像素[i],所以我假设问题不在那里。不应该是代码>删除[]/Cord>?我生锈了C++,我的疑惑是如果<代码> DEL> []/COD>调用元素上的析构函数,它确实是这样,但是这里我们有简单的指针,不是MapPixel对象或对它们的引用,所以看起来:您释放了您的dinamically创建的MapPixel对象,但没有正确地释放dinamically创建的指针数组(指向此类对象)。+1关于像素的问题。。。刚刚结束对缺乏细节的评论…:)“(因为边框的neib比中心像素少,所以此列表取决于大小)”这就是为什么我没有使用MapPixel*neib[8]。此外,分配只是开始,一旦分配,std::vector就是常量访问。关于答案,我将检查它是否修复了错误。@J.C.Leitão nah您获得了一些,即数组的内存直接集成在对象本身中,而向量元素的内存位于堆的其他位置。但是考虑到这些事情可能更像是过早优化。可能会丢失2509088字节,我认为为代码添加熵还为时过早。。。除息的
pixels = new MapPixel*[width];
for (int i = 0; i < width; i++)
    pixels[i] = new MapPixel[height];
pixels[i][j].addNeib(&pixels[i][j+1]);
for (int i = 0; i < width; i++)
    delete pixels[i];
delete pixels;
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&lt;MapPixel const*&gt;::allocate(unsigned long, void const*) in ...
  4: std::_Vector_base&lt;MapPixel const*, std::allocator&lt;MapPixel const*&gt; &gt;::_M_allocate(unsigned long) in stl_vector.h:131
  5: std::vector&lt;MapPixel const*, std::allocator&lt;MapPixel const*&gt; &gt;::_M_insert_aux(__gnu_cxx::__normal_iterator&lt;MapPixel const**, std::vector&lt;MapPixel const*, std::allocator&lt;MapPixel const*&gt; &gt; &gt;, MapPixel const* const&amp;) in vector.tcc:271
  6: std::vector&lt;MapPixel const*, std::allocator&lt;MapPixel const*&gt; &gt;::push_back(MapPixel const* const&amp;) in stl_vector.h:608
  7: MapPixel::addNeib(MapPixel const*) in mappixel.cpp:52
neib.push_back(neib_);
pixels = new MapPixel*[width];
for (int i = 0; i < width; i++)
  pixels[i] = new MapPixel[height];
for (int i = 0; i < width; i++)
  delete [] pixels[i];
delete [] pixels;
std::vector<std::vector<MapPixel> > pixels;
// in constructor something like:
pixels.resize(width, std::vector<MapPixel>(height));
// nothing to do in destructor