繁体   English   中英

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

[英]memory leak (Valgrind) on a grid of points

我正在使用class类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

其中neib是与之相邻的指向其他MapPixel的指针的列表。

我正在使用方法

void MapPixel::addNeib(const MapPixel* neib_)
{
    neib.push_back(neib_);
}

向中性像素添加一个指针以构建图形(由于边框的中枢比中心像素少,因此此列表取决于大小)。

我的过程是让一个类Map和一个成员

MapPixel **pixels;

在构造函数Map :: Map()中使用

pixels = new MapPixel*[width];
for (int i = 0; i < width; i++)
    pixels[i] = new MapPixel[height];

我使用MapPixel :: addNode()方法建立网络(例如)

pixels[i][j].addNeib(&pixels[i][j+1]);

并在Map ::〜Map()中按相反的顺序删除MapPixel(无需删除neib以避免double free):

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&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

所有与第52行有关:

neib.push_back(neib_);

有人知道吗? 现在,我对是否可以使用std :: vector构建像素的近景点失去了信心。

请注意,valgrind表示“ 可能丢失”,而不是“ 绝对丢失”。 区别很重要。 具体含义请参见此处

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

除非! 当释放pixels数组时,是否使用delete[]delete

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

正如已经提到的其他答案一样,内存泄漏很可能是由错误的delete运算符引起的。 在构造函数中,使用运算符new []创建一个数组数组:

pixels = new MapPixel*[width];
for (int i = 0; i < width; i++)
  pixels[i] = new MapPixel[height];

您需要使用相应的array-delete 运算符delete []释放阵列的内存:

for (int i = 0; i < width; i++)
  delete [] pixels[i];
delete [] pixels;

但是,我建议您使用嵌套的std::vector代替像素矩阵。 这样,您可以免费获得内存管理。

std::vector<std::vector<MapPixel> > pixels;
// in constructor something like:
pixels.resize(width, std::vector<MapPixel>(height));
// nothing to do in destructor

对于您的邻居,我不会使用std :: vector,而是使用普通的MapPixel *neib[8]; (假设摩尔邻里)或更确切地说std::array<MapPixel*, 8> neib; 但是我不知道您可能对此项目还有其他要求。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM