多线程中CString内存泄漏的解决方法。

多线程导致的内存泄漏
DWORD WINAPI ConnectionWorkerProc(LPVOID pObject)
{
CString strPath;
CString strFileName;
CString currentStr;
TCHAR currentPath[512] = _T("");
TCHAR sendPfilePath[256] = _T("");
GetCurrentDirectory(sizeof(currentPath), currentPath);

strPath=CString(currentPath);
currentStr=CString(currentPath);
strFileName=strPath + strFileName;
CString strTest;
char test[30] = "sasdsdsaasd";
        strTest = CString(test);
        while{
             Sleep(30);
        }
}

       
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {1500} normal block at

0x01A7D220, 40 bytes long.
Data: <, x            > 2C FB BF 78 0B 00 00 00 0B 00 00 00 01 00 00 00 
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {1498} normal block at

0x01A7E340, 46 bytes long.
Data: <, x            > 2C FB BF 78 0E 00 00 00 0E 00 00 00 01 00 00 00 
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {1497} normal block at

0x01A7DAB8, 46 bytes long.
Data: <, x            > 2C FB BF 78 0E 00 00 00 0E 00 00 00 01 00 00 00 
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141) : {1496} normal block at

0x01A7CCD0, 46 bytes long.

一直到程序运行结束,线程函数都没有结束,在栈上没有弹出,导致了内存泄漏。
在转化和赋值的过程,CString内部分配了内存,但是由于该函数一直没有执行结束,CString内部申请的

内存便一直没有释放掉。

解决的方法:
1,当知道线程有可能或者确定不会结束,不要在线程中使用CString的copy,assignment,add,使用TCHAR

或者char的,strcat,strcpy等,或者使用std::string,也不会造成内存泄漏
2,使用CString的时候,new和delete,CString *pStr = new CString;用完了之后delete,也可以避免
内存泄漏。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值