删除链表,内存泄漏C ++

时间:2015-12-27 18:12:13

标签: c++ memory-leaks linked-list

我写了一个链表实现。我实现了一组方法,当然还有删除列表方法。但是,当我编译它并使用Properties.Settings.Default.EmailFrom = txtbxEmailFrom.Text; 检查内存泄漏时,它会显示内存泄漏:

Valgrind-3.10.1

以下是代码:

    valgrind --leak-check=full ./listatmp
==3200== Memcheck, a memory error detector
==3200== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==3200== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==3200== Command: ./listatmp
==3200== 
3 5 7 1024 9 1024 5 34 67 
==3200== 
==3200== HEAP SUMMARY:
==3200==     in use at exit: 32 bytes in 2 blocks
==3200==   total heap usage: 12 allocs, 10 frees, 192 bytes allocated
==3200== 
==3200== 16 bytes in 1 blocks are definitely lost in loss record 1 of 2
==3200==    at 0x4C2B0E0: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3200==    by 0x400871: node(int) (listatmp.cpp:16)
==3200==    by 0x400BFA: main (listatmp.cpp:127)
==3200== 
==3200== 16 bytes in 1 blocks are definitely lost in loss record 2 of 2
==3200==    at 0x4C2B0E0: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3200==    by 0x400871: node(int) (listatmp.cpp:16)
==3200==    by 0x400C2A: main (listatmp.cpp:128)
==3200== 
==3200== LEAK SUMMARY:
==3200==    definitely lost: 32 bytes in 2 blocks
==3200==    indirectly lost: 0 bytes in 0 blocks
==3200==      possibly lost: 0 bytes in 0 blocks
==3200==    still reachable: 0 bytes in 0 blocks
==3200==         suppressed: 0 bytes in 0 blocks
==3200== 
==3200== For counts of detected and suppressed errors, rerun with: -v
==3200== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

从valgrind输出来看,问题出在方法之前/之后,但我不知道如何修复它。

1 个答案:

答案 0 :(得分:4)

  myList2 = insertBefore(myList2, node(9), node(1024));

node(9)在哪里被删除?

你应该使用的东西:std :: list,references,constructors / destructors,class methods。