vc++6.0中内存泄漏请教如何释放

chinalyylj 2009-10-27 10:11:10
CList<lvItem*, lvItem*> listItems;
POSITION listPos;

//Retrieve the selected items
POSITION pos = pDragList->GetFirstSelectedItemPosition(); //iterator for the CListCtrl
while(pos) //so long as we have a valid POSITION, we keep iterating
{
plvitem = new LVITEM; ZeroMemory(plvitem, sizeof(LVITEM));
pItem = new lvItem; //ZeroMemory(pItem, sizeof(lvItem)); //If you use ZeroMemory on the lvItem struct,
pItem->plvi = plvitem;
pItem->plvi->iItem = m_nDragIndex;
pItem->plvi->mask = LVIF_TEXT;
pItem->plvi->pszText = new char; //since this is a pointer
pItem->plvi->cchTextMax = 255;

m_nDragIndex = pDragList->GetNextSelectedItem(pos);

//Get the item
pItem->plvi->iItem = m_nDragIndex; //
pDragList->GetItem(pItem->plvi); //retrieve the information

CString str1,str2;
str1=pItem->sCol1;
str2=pItem->sCol2;
//Save the pointer to the new item in our CList
listItems.AddTail(pItem);
}
if(pDragList == pDropList) //we are reordering the list (moving)
{
//Delete the selected items
pos = pDragList->GetFirstSelectedItemPosition();
while(pos)
{

} //EO while(pos)
} //EO if(pDragList...

//Iterate through the items stored in memory and add them back into the CListCtrl at the
listPos = listItems.GetHeadPosition();
while(listPos)
{


} //EO while(listPos)

// listItems.RemoveAll();
// for(int i = 1; i < listItems.GetCount(); i ++)
// while(pItem!=NULL)
// {
// }
// while (plvitem!=NULL)
// {
// }
delete plvitem;
delete pItem;
listItems.RemoveAll();


Detected memory leaks!
Dumping objects ->
strcore.cpp(118) : {361} normal block at 0x00385A98, 269 bytes long.
Data: < Line> 01 00 00 00 06 00 00 00 00 01 00 00 4C 69 6E 65
strcore.cpp(118) : {360} normal block at 0x003857F8, 269 bytes long.
Data: < SubL> 01 00 00 00 0B 00 00 00 00 01 00 00 53 75 62 4C
D:\DlgTest_new\DlgTestDlg.cpp(351) : {359} normal block at 0x00384538, 12 bytes long.
Data: < W8 X8 Z8 > 90 57 38 00 04 58 38 00 A4 5A 38 00
D:\DlgTest_new\DlgTestDlg.cpp(349) : {358} normal block at 0x00385790, 40 bytes long.
Data: < > 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Object dump complete.


以上用new 分配的内存,我delete 后总是有内存泄漏,请高手指点迷津 谢谢!

...全文
223 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangrenhui 2009-10-28
  • 打赏
  • 举报
回复
UP
bragi523 2009-10-28
  • 打赏
  • 举报
回复
先逐个删掉对象
然后清空
ToperRay 2009-10-28
  • 打赏
  • 举报
回复
for(;;)

delete xxx;
jyh_baoding 2009-10-28
  • 打赏
  • 举报
回复
把对象逐个取出,删掉
贪玩的老鼠 2009-10-28
  • 打赏
  • 举报
回复
你是循环分配的内存,
而释放的时候只是释放最后一个,
肯定有内存泄漏.
需要循环是否listItems里分配的内存
for(int i=0; i<listItems.GetCount(); i++)
{

pItem =listItems.GetAt(i);
delete pItem->plvi->pszText
delete pItem->plvi ;
delete pItem;
}
stivenjia 2009-10-27
  • 打赏
  • 举报
回复
教给你个无敌的方法

HANDLE hHeap = NULL;
SYSTEMINFO tgSysInfo = { 0 };
GetSystemInfo(&tgSysInfo);
hHeap = CreateHeap(0,tgSysInfo.dwPageSize,0);

LPVOID lpPos = HeapAlloc(hHeap,HEAP_ZERO_MEMORY,sizeof(sizeof(LVITEM)));
plvitem = new(lpPos)LVITEM;

lpPos = HeapAlloc(hHeap,HEAP_ZERO_MEMORY,sizeof(sizeof(LVITEM)));
pItem = new(lpPos)lvItem; //ZeroMemory(pItem, sizeof(lvItem))

HeapDestory(hHeap); //注意最后一定要释放Heap管理器否则内存泄露哦

Hiiishe 2009-10-27
  • 打赏
  • 举报
回复
根据你的逻辑在不需要指针所指的对象后要delete并赋空。
new与delete成对。
muzizongheng 2009-10-27
  • 打赏
  • 举报
回复


for(int i=0; i<listItems.GetCount(); i++)
{
delete listItems.GetAt(i);
}

listItems.RemoveAll();

liumenghappy 2009-10-27
  • 打赏
  • 举报
回复
另外,如果你更改了指针的指向,那么指针原来指向的堆区就释放不了了
liumenghappy 2009-10-27
  • 打赏
  • 举报
回复
你双击内存遗漏信息
如:strcore.cpp(118) : {361} normal block at 0x00385A98, 269 bytes long.
它会帮你找到没有释放内存的地方,然后再delete
  ::首页 >> 文档心 >> 在线杂志 >> 属性页 [ 在线杂志 第13期 ]

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧