免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3982 | 回复: 10
打印 上一主题 下一主题

stl 有内存泄漏吗?(高人指点一下) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-09-28 14:12 |只看该作者 |倒序浏览
void testerr()
{
   std::vector<std::string>; vcon
   for(int i=0;i<1000000;++i)
     vcon.push_back("asdfasdfsd";
}

void testok()
{
  std::string ka="asdfasdfsd";
  std::vector<std::string>; vcon
  for(int i=0;i<1000000;++i)
    vcon.push_back(ka);
}
int main(int argc,char)
{
        testerr();//or testok
        getchar();
        return 123;
}

linux: redhat 8.0

can not see program release the memory.

but testok can release the memory.

note:use top tool to see the memory

论坛徽章:
0
2 [报告]
发表于 2005-09-28 14:49 |只看该作者

stl 有内存泄漏吗?(高人指点一下)

应该都没有问题吧,按照C++的风格,它应该在析构函数部分完成内存释放工作.
如果你不放心加一个 vcon.clear()

论坛徽章:
0
3 [报告]
发表于 2005-09-28 16:03 |只看该作者

stl 有内存泄漏吗?(高人指点一下)

有没有高人啊

论坛徽章:
0
4 [报告]
发表于 2005-09-28 17:04 |只看该作者

stl 有内存泄漏吗?(高人指点一下)

问一下,怎么看出来release或者没有release的。

论坛徽章:
0
5 [报告]
发表于 2005-09-28 18:40 |只看该作者

stl 有内存泄漏吗?(高人指点一下)

linux下可以用内存泄漏用 valgrind 检查。
你从TOP根本就看不出是否会产生内存泄漏。

另外,上面的语句不会产生内存泄漏,除非你中奖使用以前的某个版本的stl。

论坛徽章:
0
6 [报告]
发表于 2005-09-28 18:44 |只看该作者

stl 有内存泄漏吗?(高人指点一下)

不懂啊,为何 testok 可以看到内存使用下降,而testerr却看不到呢?

论坛徽章:
0
7 [报告]
发表于 2005-09-28 18:48 |只看该作者

stl 有内存泄漏吗?(高人指点一下)

系统有可能把已分配的内存在进程退出后作为系统缓存区使用。

论坛徽章:
0
8 [报告]
发表于 2005-09-28 20:09 |只看该作者

stl 有内存泄漏吗?(高人指点一下)

stl是C++的标准,只要是标准就应该没有问题!但按标准实现的程序就不一定了!!!

论坛徽章:
0
9 [报告]
发表于 2005-09-29 12:28 |只看该作者

stl 有内存泄漏吗?(高人指点一下)

没有令人满意的回答

论坛徽章:
0
10 [报告]
发表于 2005-09-29 13:57 |只看该作者

stl 有内存泄漏吗?(高人指点一下)

不知道你的产生内存泄漏的结论是如何的出来的.能不能给提供个数据看一下.


下面使用valgrind检测的结果:

  1. [root@localhost prog]# valgrind -v memoryLeak
  2. ==2776== Memcheck, a memory error detector.
  3. ==2776== Copyright (C) 2002-2005, and GNU GPL'd, by Julian Seward et al.
  4. ==2776== Using LibVEX rev 1313, a library for dynamic binary translation.
  5. ==2776== Copyright (C) 2004-2005, and GNU GPL'd, by OpenWorks LLP.
  6. ==2776== Using valgrind-3.0.0, a dynamic binary instrumentation framework.
  7. ==2776== Copyright (C) 2000-2005, and GNU GPL'd, by Julian Seward et al.
  8. --2776-- Valgrind library directory: /usr/lib/valgrind
  9. --2776-- Command line
  10. --2776--    memoryLeak
  11. --2776-- Startup, with flags:
  12. --2776--    -v
  13. --2776-- Contents of /proc/version:
  14. --2776--   Linux version 2.6.13-1.1535_FC5 (bhcompile@bugs.build.redhat.com) (gcc version 4.0.1 20050831 (Red Hat 4.0.1-12)) #1 Sun Sep 4 02:20:28 EDT 2005
  15. --2776-- Reading syms from /root/prog/memoryLeak (0x8048000)
  16. --2776-- Reading syms from /lib/ld-2.3.90.so (0x1B8E4000)
  17. --2776-- Reading syms from /usr/lib/valgrind/stage2 (0xB0000000)
  18. --2776-- Reading suppressions file: /usr/lib/valgrind/default.supp
  19. ==2776==
  20. --2776-- Reading syms from /usr/lib/valgrind/vg_preload_core.so (0x1B8FF000)
  21. --2776-- Reading syms from /usr/lib/valgrind/vgpreload_memcheck.so (0x1B902000)
  22. --2776-- REDIR: 0x1B8F8DB0 (index) redirected to 0x1B905084 (index)
  23. --2776-- Reading syms from /lib/libsafe.so.2.0.16c (0x1B908000)
  24. --2776--    object doesn't have a symbol table
  25. --2776-- Reading syms from /usr/lib/libstdc++.so.6.0.6 (0x1B934000)
  26. --2776--    object doesn't have a symbol table
  27. --2776-- Reading syms from /lib/libm-2.3.90.so (0x1BA1D000)
  28. --2776-- Reading syms from /lib/libgcc_s-4.0.1-20050831.so.1 (0x1BA42000)
  29. --2776--    object doesn't have a symbol table
  30. --2776-- Reading syms from /lib/libc-2.3.90.so (0x1BA4C000)
  31. --2776-- Reading syms from /lib/libdl-2.3.90.so (0x1BB78000)
  32. --2776-- REDIR: 0x1BAB6D60 (rindex) redirected to 0x1B904F94 (rindex)
  33. --2776-- REDIR: 0x1B8E47F0 (_dl_sysinfo_int80) redirected to 0xB0022B03 (???)
  34. --2776-- REDIR: 0x1BAB1BFE (malloc) redirected to 0x1B903793 (malloc)
  35. --2776-- REDIR: 0x1BAAFDC8 (free) redirected to 0x1B9042B2 (free)
  36. --2776-- REDIR: 0x1BAB7BD0 (memset) redirected to 0x1B9053D8 (memset)
  37. --2776-- REDIR: 0x1B9E8890 (operator new(unsigned)) redirected to 0x1B903B2E (operator new(unsigned))
  38. --2776-- REDIR: 0x1BAB8030 (memcpy) redirected to 0x1B905490 (memcpy)
  39. --2776-- REDIR: 0x1B9E7308 (operator delete(void*)) redirected to 0x1B904546 (operator delete(void*))
  40. ==2776==
  41. ==2776== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 26 from 2)
  42. --2776--
  43. --2776-- supp:    3 index-not-intercepted-early-enough-HACK-1
  44. --2776-- supp:   23 dl_relocate_object
  45. ==2776== malloc/free: in use at exit: 40417236 bytes in 9920 blocks.
  46. ==2776== malloc/free: 9951 allocs, 31 frees, 57194292 bytes allocated.
  47. ==2776==
  48. ==2776== searching for pointers to 9920 not-freed blocks.
  49. ==2776== checked 40536016 bytes.
  50. ==2776==
  51. ==2776== LEAK SUMMARY:
  52. [color=red]==2776==    definitely lost: 0 bytes in 0 blocks.
  53. ==2776==      possibly lost: 0 bytes in 0 blocks.
  54. ==2776==    still reachable: 40417236 bytes in 9920 blocks.
  55. ==2776==         suppressed: 0 bytes in 0 blocks.
  56. ==2776== Reachable blocks (those to which a pointer was found) are not shown.[/color]
  57. ==2776== To see them, rerun with: --show-reachable=yes
  58. --2776--  memcheck: sanity checks: 5229 cheap, 210 expensive
  59. --2776--  memcheck: auxmaps: 0 auxmap entries (0k, 0M) in use
  60. --2776--  memcheck: auxmaps: 0 searches, 0 comparisons
  61. --2776--  memcheck: secondaries: 765 issued (48960k, 47M)
  62. --2776--  memcheck: secondaries: 33 accessible and distinguished (2112k, 2M)
  63. --2776--     tt/tc: 5649 tt lookups requiring 5788 probes
  64. --2776--     tt/tc: 5649 fast-cache updates, 3 flushes
  65. --2776-- translate: new        2706 (60927 ->; 991661; ratio 162:10) [0 scs]
  66. --2776-- translate: dumped     0 (0 ->; ??)
  67. --2776-- translate: discarded  2 (95 ->; ??)
  68. --2776-- scheduler: 261466397 jumps (bb entries).
  69. --2776-- scheduler: 5229/18233 major/minor sched events.
  70. --2776--    sanity: 5230 cheap, 210 expensive checks.
  71. --2776--    exectx: 4999 lists, 26 contexts (avg 0 per list)
  72. --2776--    exectx: 10008 searches, 9982 full compares (997 per 1000)
  73. --2776--    exectx: 0 cmp2, 81 cmp4, 0 cmpAll
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP