以前用vc++6.0写的一个程序现在用vs2005运行出现大量的类似下面的内存泄露问题。
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {33188} normal block at 0x0189A7F0, 29 bytes long.
Data: < >x > EC 97 3E 78 0C 00 00 00 0C 00 00 00 01 00 00 00
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {33187} normal block at 0x0189A798, 23 bytes long.
Data: < >x > EC 97 3E 78 06 00 00 00 06 00 00 00 01 00 00 00
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {33186} normal block at 0x0189A738, 29 bytes long.
Data: < >x > EC 97 3E 78 0C 00 00 00 0C 00 00 00 01 00 00 00
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {33185} normal block at 0x01891BD8, 25 bytes long.
Data: < >x > EC 97 3E 78 08 00 00 00 08 00 00 00 01 00 00 00
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {33184} normal block at 0x01891B78, 29 bytes long.
Data: < >x > EC 97 3E 78 0C 00 00 00 0C 00 00 00 01 00 00 00
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {33183} normal block at 0x01890408, 23 bytes long.
Data: < >x > EC 97 3E 78 06 00 00 00 06 00 00 00 01 00 00 00
程序里用到很多CString和CStringArray类型的数据,new和delete也是配套的,就几处用到GDI对象也都释放了资源,查网上资料说是由CString引起的,难道我要把所有的CString和CStringArray类型数据替换掉,可是这样的数据太多了呀,大家有没有好的办法。
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {33188} normal block at 0x0189A7F0, 29 bytes long.
Data: < >x > EC 97 3E 78 0C 00 00 00 0C 00 00 00 01 00 00 00
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {33187} normal block at 0x0189A798, 23 bytes long.
Data: < >x > EC 97 3E 78 06 00 00 00 06 00 00 00 01 00 00 00
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {33186} normal block at 0x0189A738, 29 bytes long.
Data: < >x > EC 97 3E 78 0C 00 00 00 0C 00 00 00 01 00 00 00
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {33185} normal block at 0x01891BD8, 25 bytes long.
Data: < >x > EC 97 3E 78 08 00 00 00 08 00 00 00 01 00 00 00
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {33184} normal block at 0x01891B78, 29 bytes long.
Data: < >x > EC 97 3E 78 0C 00 00 00 0C 00 00 00 01 00 00 00
f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {33183} normal block at 0x01890408, 23 bytes long.
Data: < >x > EC 97 3E 78 06 00 00 00 06 00 00 00 01 00 00 00
程序里用到很多CString和CStringArray类型的数据,new和delete也是配套的,就几处用到GDI对象也都释放了资源,查网上资料说是由CString引起的,难道我要把所有的CString和CStringArray类型数据替换掉,可是这样的数据太多了呀,大家有没有好的办法。
解决方案 »
- 窗体最大化后,状态栏怎么跟着变化呀??
- HELP:无模式对话框消息响应
- 怎样在对话框中将FLASH文件显示为透明背景?
- 问高手一个问题
- 在vc中有没有象c++ builder中的表控件TTable?
- 服务器端如何知道自己这边网络故障?
- 我不想在button显示字符,用图片代替,不知该怎么做?
- 关于Dll导入出错的问题…………各路英雄请留步!!!!!!!!!!!!!!(二百分)
- 高分求计算行列式的值的方法
- MFC对话框中添加可拖动的工具栏
- 学MFC不到一个月,想听听各位前辈们的看法、想法、、、、未来前景
- QQ是VC2005写的,麦库都是Delphi写的,其他常用软件都是什么编译器呢,欢迎知道的来补全?
#ifdef _DEBUG
#define new DEBUG_NEW
、没有这个宏定义的,加上!基本可以直接看到了!
#define CString int
你原有的CString定义的变量,如:CString name;,就不用改了。
不知道可行否。
你贴出来的里面给出的.cpp的line 141 处发生内存泄露,这里分配的内存没释放,
看看后面是不是有分支语句 , 只把一部分释放了。VS2005是处理内存泄漏比较简单的了。
这是不是说明是有申请的堆栈没有释放呢??
搜 new 和 delete 关键词,是否完全配对
{
int type;
if(str1=="131k")
{
m_PolygonColor=RGB(63,118,100);
m_Property="可调整有林地";
}
if(str1=="121k")
{
m_PolygonColor=RGB(138,199,133);
m_Property="可调整果园";
}
if(str1=="136k")
{
m_PolygonColor=RGB(119,152,73);
m_Property="可调整苗圃";
}
else
{
type=atoi(str1);
switch(type)
{
case 201:
m_PolygonColor=RGB(108,109,101);
m_Property="城市";
break;
case 114:
m_PolygonColor=RGB(142,147,85);
m_Property="旱地";
break;
case 321:
m_PolygonColor=RGB(56,141,69);
m_Property="河流水面";
break;
case 262:
m_PolygonColor=RGB(85,98,83);
m_Property="公路用地";
break;
case 121:
m_PolygonColor=RGB(158,194,84);
m_Property="果园";
break;
case 136:
m_PolygonColor=RGB(117,215,124);
m_Property="苗圃";
break;
case 113:
m_PolygonColor=RGB(91,126,78);
m_Property="水浇地";
break;
case 317:
m_PolygonColor=RGB(115,119,113);
m_Property="其他未利用土地";
break;
case 131:
m_PolygonColor=RGB(48,184,58);
m_Property="有林地";
break;
case 203:
m_PolygonColor=RGB(83,134,149);
m_Property="农村居民地";
break;
case 156:
m_PolygonColor=RGB(79,153,98);
m_Property="农田水利用地";
break;
case 323:
m_PolygonColor=RGB(167,167,65);
m_Property="苇地";
break;
case 154:
m_PolygonColor=RGB(151,207,137);
m_Property="坑塘水面";
break;
case 151:
m_PolygonColor=RGB(176,182,158);
m_Property="畜禽饲养地";
break;
case 314:
m_PolygonColor=RGB(190,194,137);
m_Property="沙地";
break;
case 115:
m_PolygonColor=RGB(120,209,120);
m_Property="菜地";
break;
case 155:
m_PolygonColor=RGB(181,158,98);
m_Property="养殖水面";
break;
case 157:
m_PolygonColor=RGB(112,169,173);
m_Property="田坎";
break;
case 111:
m_PolygonColor=RGB(130,196,181);
m_Property="灌溉水田";
break;
case 261:
m_PolygonColor=RGB(102,72,80);
m_Property="铁路用地";
break;
default:
break;
}
}
其中m_PolygonColor是COLORREF类型数据,m_Property是CString类型数据。
问题就出在这一段代码上,当我把这段代码注释掉后,就不会有内存泄露提示,不明白这段程序为什么会有泄露,请高手指点...............
在VC++6中,CString 的拷贝构造函数没有使用内存分配,而是使用的引用,它内部保存了一个引用的计数器
比如:
CString str1="aaa";
CString str2=str1; //注意,这时候str2并没有调用 new ,而是使用str1的引用同时,str1中保存的引用记数++
str2="abcd1234"; //好了,现在str2才分配内存,str1引用记数--。这同时也是为什么str2.Empty()就没有内存泄露的原因。因为str1的引用记数也--了。另外,它分配内存的方法是4字节对齐的64字节的倍数+sizeof(内部结构)(超过64的时候)。在多数情况下,比较简单的使用过程中,MFC的这个BUG不会发作,也就是不会有内存泄露。那什么时候CString会暴露出BUG那?
我以前出现错误的经验是:如果多次调用带有CString引用的参数的函数(形如:funstr(CString &str);这样的函数),在一定的时候(和字符串长度有关系),CString的内部引用记数器发生记数混乱,造成内存泄露。
COLORREF m_PolygonColor;//私有成员变量
CString m_Property;//公有成员变量
在构造函数里已经初始化
m_PolygonColor=RGB(255,255,255);
m_Property="";