IはmapPixelの概念を用いて、マップグリッド(点の2次元離散的な数)を作成していクラス:neibはリストであるメモリリーク(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
それに隣接する他のMapPixelsへのポインタ。
Iグラフを構築するneiberピクセルへのポインタを追加する方法を
void MapPixel::addNeib(const MapPixel* neib_)
{
neib.push_back(neib_);
}
を使用している(境界が中心画素未満neibsを有しているので、このリストは、サイズに依存します)。
マイ手続きコンストラクタ地図で
MapPixel **pixels;
メンバーを持つクラスマップを持つことである::マップ()
pixels = new MapPixel*[width];
for (int i = 0; i < width; i++)
pixels[i] = new MapPixel[height];
を使用して、私は方法のMapPixel ::てaddNodeを(使用)へ
となります。Map ::〜Map()では逆の順序でMapPixelを削除します(削除する必要はありません)。 neibs二重解放を避けるために):
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に関連するすべて:
for (int i = 0; i < width; i++)
delete pixels[i];
delete pixels;
Valgrindのは、このようないくつかの大きなメモリリークがあると言う
neib.push_back(neib_);
誰もがこれを理解しています?今私はstd :: vectorを使ってピクセルのneibを構築できるかどうか確信がなくなりました。
メモリリークの可能性のある問題では、alloc(new)とdealloc(delete)の両方の方法を教えてください。例えば。 MaxPixelを逆の順序で削除します。あなたはピクセルそのものですか?私は答えがイエスであり、与えられた答えが当てはまると思います。しかし、見るために完全な新しい/削除コードを持つ方が良いでしょう! – ShinTakezou
に削除コードが追加されました。私はValgrindが新しいピクセル/新しいピクセル[i]に不平を言うことはないので、それが原因ではないと言います。なぜそれが問題ではないと仮定したのでしょうか。 –
は代わりに 'delete []'にするべきではありませんか? – ShinTakezou