大家给看一下下列代码有内存泄露 - C++ Builder / Windows SDK/API
int __fastcall WatchMointor(char ppExeName[], int kill)
{
String ProcessName;
int ProcessSize;
bool ContinueLoop;
HANDLE FSnapshotHandle;
TProcessEntry32 FProcessEntry32;
char tmpExeName[MAX_WATCHEXE_NUM];
HANDLE pHand;
FSnapshotHandle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize = sizeof(FProcessEntry32);
ContinueLoop = Process32First(FSnapshotHandle,&FProcessEntry32);
// 获取进程列表
while (ContinueLoop)
{
ProcessName = FProcessEntry32.szExeFile;
StrPCopy(tmpExeName, ProcessName);
if (stricmp(ppExeName, tmpExeName)==0)
{
if (kill == 1)
{
pHand = OpenProcess(1,false,FProcessEntry32.th32ProcessID);
if (pHand!=NULL)
TerminateProcess(pHand,-1);
CloseHandle(pHand);
pHand = NULL;
}
return 0;
}
ContinueLoop = Process32Next(FSnapshotHandle,&FProcessEntry32);
}
return -1;
}
[解决办法]
自己放CodeGuard看内存漏洞吧。
[解决办法]
在函数返回的时候,要加上
CloseHandle(FSnapshotHandle);
否则从进程管理器中可以看出,多次执行此函数,句柄在不断增加....
[解决办法]
同意4楼意见。