重複の可能性:
3 つのルールとは?
std::pairコンポーネントのデストラクタをどのくらい正確に呼び出しますか? クラスのインスタンスを に追加しようとしていますが、クラスstd::mapのデストラクタに関するエラーが発生しています。
質問/問題を次の非常に単純な例に絞り込みました。
以下では、構築時に配列をmy_class作成しint、破棄時に削除するだけです。どういうわけか、「二重削除」エラーが発生します。
//my_class.h
class my_class {
public:
int an_int;
int *array;
//constructors:
my_class()
{
array = new int[2];
}
my_class(int new_int) : an_int(new_int)
{
array = new int[2];
}
//destructor:
~my_class()
{
delete[] array;
}
}; //end of my_class
一方、main.cpp では...
//main.cpp
int main(int argc, char* argv[])
{
std::map<int, my_class> my_map;
my_map.insert( std::make_pair<int, my_class> (1, my_class(71) ) );
return 0;
} // end main
コンパイルは問題なく行われますが、これにより次のランタイム エラーが発生します。
*** glibc detected *** ./experimental_code: double free or corruption (fasttop):
または、valgrind を使用して:
==15258== Invalid free() / delete / delete[] / realloc()
==15258== at 0x40249D7: operator delete[](void*) (vg_replace_malloc.c:490)
==15258== by 0x8048B99: main (my_class.h:38)
==15258== Address 0x42d6028 is 0 bytes inside a block of size 8 free'd
==15258== at 0x40249D7: operator delete[](void*) (vg_replace_malloc.c:490)
==15258== by 0x8048B91: main (my_class.h:38)
(コメントなどを切り取ったため、行番号はオフになっています)
私は何かが欠けているに違いないstd::pair...?
事前にすべてに感謝します!