以下のコードで、メモリリークが検知されてしまいます。
intsが保持しているアドレスは*aと等しいにも関わらず、deleteしてもメモリリークが発生してしまいます。
どのようにしたら、メモリリークを回避できますでしょうか。
#include <vector> // メモリリーク用 #ifdef _DEBUG #define _CRTDBG_MAP_ALLOC #include <stdlib.h> #include <crtdbg.h> #endif int main(){ int *a = new int; std::vector<int*> ints; ints.push_back(a); delete ints[0]; // メモリリーク検査 #ifdef _DEBUG _CrtDumpMemoryLeaks(); #endif }検知内容:
Detected memory leaks!
Dumping objects ->
{125} normal block at 0x00214EC0, 4 bytes long.
Data: < N! > 80 4E 21 00
Object dump complete.