STL List push_back 会不会产生内存泄露

C++语言 码拜 8年前 (2016-01-29) 1318次浏览
随便写了一些代码,debug下测试内存问题。
int main()
{
list<string> strList,tempList;
strList.push_back(“dnc1”);
strList.push_back(“dnc2”);
strList.push_back(“dnc10”);
strList.push_back(“dnc12”);
strList.sort();
list<string>::iterator itr;
cout<<“默认排序………”<<endl;
for (itr=strList.begin();itr!=strList.end();++itr)
{
cout<<*itr<<endl;
}
//strList.clear();
_CrtDumpMemoryLeaks();
return 0;
}
(1)这样写会不会产生内存泄露?
测试结果:
“sortTest.exe”: 已加载“E:\example\sortTest\Debug\sortTest.exe”,已加载符号。
“sortTest.exe”: 已加载“C:\Windows\System32\ntdll.dll”,已加载符号(去除源信息)。
“sortTest.exe”: 已加载“C:\Windows\System32\kernel32.dll”,已加载符号(去除源信息)。
“sortTest.exe”: 已加载“C:\Windows\System32\KernelBase.dll”,已加载符号(去除源信息)。
“sortTest.exe”: 已加载“C:\Windows\System32\msvcp100d.dll”,已加载符号(去除源信息)。
“sortTest.exe”: 已加载“C:\Windows\System32\msvcr100d.dll”,已加载符号(去除源信息)。
Detected memory leaks!
Dumping objects ->
{140} normal block at 0x004C4A30, 8 bytes long.
Data: < IL     > D0 49 4C 00 00 00 00 00
{139} normal block at 0x004C49C8, 40 bytes long.
Data: <hHL  IL 0JL dnc1> 68 48 4C 00 18 49 4C 00 30 4A 4C 00 64 6E 63 31
{137} normal block at 0x004C4980, 8 bytes long.
Data: < IL     > 20 49 4C 00 00 00 00 00
{136} normal block at 0x004C4918, 40 bytes long.
Data: < IL  GL  IL dnc1> C8 49 4C 00 B8 47 4C 00 80 49 4C 00 64 6E 63 31
{134} normal block at 0x004C48D0, 8 bytes long.
Data: <pHL     > 70 48 4C 00 00 00 00 00
{133} normal block at 0x004C4868, 40 bytes long.
Data: <XFL  IL  HL dnc2> 58 46 4C 00 C8 49 4C 00 D0 48 4C 00 64 6E 63 32
{131} normal block at 0x004C4820, 8 bytes long.
Data: < GL     > C0 47 4C 00 00 00 00 00
{130} normal block at 0x004C47B8, 40 bytes long.
Data: < IL XFL  HL dnc1> 18 49 4C 00 58 46 4C 00 20 48 4C 00 64 6E 63 31
{128} normal block at 0x004C4708, 40 bytes long.
Data: < GL  GL         > 08 47 4C 00 08 47 4C 00 CD CD CD CD CD CD CD CD
{127} normal block at 0x004C46C0, 8 bytes long.
Data: <        > A0 F9 1F 00 00 00 00 00
{126} normal block at 0x004C4658, 40 bytes long.
Data: < GL hHL         > B8 47 4C 00 68 48 4C 00 CD CD CD CD CD CD CD CD
{125} normal block at 0x004C4610, 8 bytes long.
Data: <        > B8 F9 1F 00 8C F9 1F 00
Object dump complete.

(2)发现内存泄露的原因是什么?

当本人取消注释的  strList.clear();

测试结果:
“sortTest.exe”: 已加载“E:\example\sortTest\Debug\sortTest.exe”,已加载符号。
“sortTest.exe”: 已加载“C:\Windows\System32\ntdll.dll”,已加载符号(去除源信息)。
“sortTest.exe”: 已加载“C:\Windows\System32\kernel32.dll”,已加载符号(去除源信息)。
“sortTest.exe”: 已加载“C:\Windows\System32\KernelBase.dll”,已加载符号(去除源信息)。
“sortTest.exe”: 已加载“C:\Windows\System32\msvcp100d.dll”,已加载符号(去除源信息)。
“sortTest.exe”: 已加载“C:\Windows\System32\msvcr100d.dll”,已加载符号(去除源信息)。
Detected memory leaks!
Dumping objects ->
{128} normal block at 0x00314708, 40 bytes long.
Data: < G1  G1         > 08 47 31 00 08 47 31 00 CD CD CD CD CD CD CD CD
{127} normal block at 0x003146C0, 8 bytes long.
Data: <        > AC FA 1B 00 00 00 00 00
{126} normal block at 0x00314658, 40 bytes long.
Data: <XF1 XF1         > 58 46 31 00 58 46 31 00 CD CD CD CD CD CD CD CD
{125} normal block at 0x00314610, 8 bytes long.
Data: <        > C4 FA 1B 00 98 FA 1B 00
Object dump complete.


(3)发现的内存泄露为什么和上一次不一样?在这里 strList.clear()起到的作用是什么?
解决方案:20分
理解讨论之前请先学会怎么样观察
计算机组成原理→DOS命令→汇编语言→C语言(不包括C++)、代码书写规范→数据结构、编译原理、操作系统→计算机网络、数据库原理、正则表达式→其它语言(包括C++)、架构……
对学习编程者的忠告:
多用小脑和手,少用大脑、眼睛和嘴,会更快地学会编程!
眼过千遍不如手过一遍!
书看千行不如手敲一行!
手敲千行不如单步一行!
单步源代码千行不如单步Debug版对应汇编一行!
单步Debug版对应汇编千行不如单步Release版对应汇编一行!
不会单步Release版对应汇编?在你想单步Release版C/C++代码片断的前面临时加一句DebugBreak();重建全部,然后在IDE中运行。(一般人本人不告诉他!)
单步类的实例“构造”或“复制”或“作为函数参数”或“作为函数返回值返回”或“参加各种运算”或“退出作用域”的语句对应的汇编代码几步后,就会来到该类的“构造函数”或“复制构造函数”或“运算符重载”或“析构函数”对应的C/C++源代码处。
VC调试时按Alt+8、Alt+7、Alt+6和Alt+5,打开汇编窗口、堆栈窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应堆栈、内存和寄存器变化,这样过一遍不就啥都明白了吗。
对VC来说,所谓‘调试时’就是编译连接通过以后,按F10或F11键单步执行一步以后的时候,或在某行按F9设了断点后按F5执行停在该断点处的时候。
解决方案:20分
放心吧,不会有内存泄露,
一般来说,内存使用原则是谁申请谁释放
你只要保证本人的c++代码new的内存正确delete掉,
STL库函数和对象申请的内存会本人释放。
你就算不执行strList.clear();程序结束也会自动释放list申请的内存,
原因是 你的strList,tempList都是main中的局部对象变量,在main结束的时候,会自动执行对象的析构函数,假如你看list的源码,就会发现list在析构函数中执行了clear.

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明STL List push_back 会不会产生内存泄露
喜欢 (0)
[1034331897@qq.com]
分享 (0)