当程序运行到下面的循环是就每3秒增加4K内存? 请高人给我看看是不是哪里有内存泄漏之类的错误。
while(TRUE)
{ //设置起始查询时间
SqlMaxTime.Format(_T("select  max(FTime) as aa from tim_InOut"));
try
{                
rs1.Open(AFX_DB_USE_DEFAULT_TYPE,SqlMaxTime);  //打开查询记录
rs1.GetFieldValue(_T("aa"),m_strBeginTime);
rs1.Close();
if (m_strBeginTime.IsEmpty())
{
m_strBeginTime.Format(_T("2009-01-01 01:01:01"));
} }
catch(CDBException * ex)
{
AfxMessageBox(ex->m_strError);
AfxMessageBox(ex->m_strStateNativeOrigin); }
catch(CException * e)
{
TCHAR szError[100];
e->GetErrorMessage(szError,100);
AfxMessageBox(szError);


for (i=0;i<sCount;i++)
{
//显示和存取数据
/*CFaceOnlineDlg *pFaceOnlineDlg = (CFaceOnlineDlg *)pParam;
ASSERT(pFaceOnlineDlg != NULL);*/

PROTOCOL_HEAD ph;
ph.flag = htons(0x6666);
ph.index = htons(4);
ph.hType = GET;
ph.pType = ACCESS;
ph.dataLen = htonl(sizeof(RECORD_REQUEST)); RECORD_REQUEST rq;
rq.userid=0; SYSTEMTIME tm;
GetLocalTime(&tm);
m_strEndTime.Format(_T("%04d-%02d-%02d %02d:%02d:%02d"),tm.wYear,tm.wMonth,tm.wDay,tm.wHour,tm.wMinute,tm.wSecond);
USES_CONVERSION;
strcpy(rq.begintime,T2A(m_strBeginTime.GetBuffer(0)));
strcpy(rq.endtime, T2A(m_strEndTime.GetBuffer(0)));
m_strBeginTime.ReleaseBuffer();
m_strEndTime.ReleaseBuffer(); rq.recordtype=0;
rq.isphoto=0;
int userid = rq.userid;
userid = htonl(userid);
memcpy(ph.data, (char *)&userid, 4); RECORD_REQUEST * prr = (RECORD_REQUEST *)ph.data;
prr->userid = htonl(rq.userid);
sprintf(prr->begintime, rq.begintime); 
sprintf(prr->endtime,rq.endtime);
prr->recordtype =rq.recordtype;
prr->isphoto = rq.isphoto; int iRe = send(pFaceOnlineDlg->m_ConnectSock[i], (char *)&ph, HEADLEN+70, 0);// 发送请求记录个数
if (iRe>0)
{
/*pFaceOnlineDlg->ShowMsg("请求记录条数");*/ } if (SOCKET_Select(pFaceOnlineDlg->m_ConnectSock[i]))//接受回复
{
CHAR sdBuf[MAX_BUF_SIZE] = {0};
int iRetc = recv(pFaceOnlineDlg->m_ConnectSock[i], (char *)sdBuf, MAX_BUF_SIZE, 0); if (iRetc>0)
{
PROTOCOL_HEAD *ppt = (PROTOCOL_HEAD *)sdBuf; }
int count;
int nCount=0;
count = *((int*)&sdBuf[10]);
count = ntohl(count); 
nCount = count;
RECORD_INFO*ppRi = (RECORD_INFO *)LocalAlloc(LPTR,sizeof(RECORD_INFO) * nCount); /*CString str;
str.Format("%d",count);
pFaceOnlineDlg->ShowMsg("记录条数:"+str);*/
int x;
for (x=0; x<count; x++)
{
ph.hType = GET_NEXT;
ph.pType = 0;
ph.dataLen = htonl(0); int iRe = send(pFaceOnlineDlg->m_ConnectSock[i], (char *)&ph, HEADLEN, 0);
if (iRe>0)
{
} if (SOCKET_Select(pFaceOnlineDlg->m_ConnectSock[i]))
{
CHAR seBuf[MAX_BUF_SIZE] = {0};
int iRetc = recv(pFaceOnlineDlg->m_ConnectSock[i], (char *)seBuf, MAX_BUF_SIZE, 0); if (iRetc>0)
{
PROTOCOL_HEAD *ppt = (PROTOCOL_HEAD *)seBuf;
/*CString strTemp;
strTemp.Format("%x",ppt->pType); pFaceOnlineDlg->ShowMsg(strTemp);*/
ppt->dataLen = ntohl(ppt->dataLen);
RECORD_INFO* pRecInfo = (RECORD_INFO*)&ppt->data;
pRecInfo->photolen = ntohl(pRecInfo->photolen);
/*pRecInfo->score = ntohl(pRecInfo->score);*/
pRecInfo->userid = ntohl(pRecInfo->userid);
/*pRecInfo->status=ntohl(pRecInfo->status);*/
/*pRecInfo->rdtime=ntohl(pRecInfo->rdtime);*/
int a,c,e; a=*((int*)&pRecInfo->userid); c=*((int*)&pRecInfo->photolen); e=*((int*)&pRecInfo->status);
CString b;
b   =   CString(pRecInfo->rdtime);
CString d;
d   =   CString(pRecInfo->cardno); CString str1,str2,str3,str4,str5,str6,str7;
Sqlcardno.Format(_T("select * from mst_CardInfo where FCardID='%d'"),a);
try
{                
rs2.Open(AFX_DB_USE_DEFAULT_TYPE,Sqlcardno);  //打开查询记录
rs2.GetFieldValue(_T("FCardNo"),str6);
rs2.Close();
}
catch(CDBException * ex)
{
AfxMessageBox(ex->m_strError);
AfxMessageBox(ex->m_strStateNativeOrigin); }
catch(CException * e)
{
TCHAR szError[100];
e->GetErrorMessage(szError,100);
AfxMessageBox(szError);
}  str1.Format(strSiteName[i]);
str2.Format(strServIp[i]);
str3.Format(_T("%s"),b.Mid(0,10));
str4.Format(_T("%s"),b.Mid(11,19));
str5.Format(_T("%d"),a);
/*str7.Format(_T("%s"),FInOut[i]);*/ int K;
K=_wtoi(FInOut[i]);
if (K==9)
{
str7.Format(_T("OUT"));
}
else if (K==8)
{
str7.Format(_T("IN"));
}
else if (K==7)
{
str7.Format(_T("I/O"));
}
if (a>0)
{
pFaceOnlineDlg->ShowMsg(str1,str2,str3,str4,str5,str6,str7);
CString   Sql; Sql.Format(_T("if not exists (select * from tim_InOut where FSign='%d' and FTime='%s') insert into tim_InOut (FSign,FTime,FDate,FCardNo,FInOut,FReaderID,FSiteID) values ('%d','%s','%s','%s','%s','%s','%s')"),a,b,a,b,str3,str6,FInOut[i],FReaderID[i],FID);
try
{  m_db.ExecuteSQL(Sql); }
catch(CDBException * ex)
{
AfxMessageBox(ex->m_strError);
AfxMessageBox(ex->m_strStateNativeOrigin);
}
catch(CException * e)
{
TCHAR szError[100];
e->GetErrorMessage(szError,100);
AfxMessageBox(szError);
}  }
/*}*/ }
}
} }

 }

Sleep(100);
CFaceOnlineApp *app = (CFaceOnlineApp *)AfxGetApp();

if (app->back==1)
{
for (i=0;i<sCount;i++)
{
/*closesocket(pFaceOnlineDlg->m_ConnectSock[i]);*/
BOOL bDontLinger = FALSE;
setsockopt(pFaceOnlineDlg->m_ConnectSock[i],SOL_SOCKET,SO_DONTLINGER,(const char*)&bDontLinger,sizeof(BOOL));
}

pFaceOnlineDlg->ShowMsg(_T("斷開接收"));
app->back=0;
break;
}
}

解决方案 »

  1.   


    怎么说呢 我设置了4分钟就重新连接  那时候就会释放点内存,如果点击最小化到托盘的话就会释放很多内存,但是一直不动它的话总的来说是一直增加的,一个晚上大概会到30M。刚开的时候是5M左右,  数据库我后面跳出循环也都关闭了,                            rs.Close();
    rsSiteCount.Close();
    rs1.Close();
    rs2.Close();
    rsconnt.Close();
    rsReaderID.Close();
    m_db.Close();这样能不能释放内存呢?
      

  2.   

    以前有人发过一个贴解决COM内存泄漏的,现在找不到了。参考下这个,不知道对你有没用。
    http://blog.csdn.net/cplusplus_zk/archive/2009/11/29/4899194.aspx
      

  3.   

    太长
    RECORD_INFO*ppRi = (RECORD_INFO *)LocalAlloc(LPTR,sizeof(RECORD_INFO) * nCount);
    貌似没看到释放
      

  4.   

    LocalAlloc()这个函数看看MSDN说明
      

  5.   

    LocalAlloc申请内存,要使用LocalFree进行释放
      

  6.   


    对噢  不过好像加上了LocalFree(ppRi)后依然内存在升