Memory Leak

R

Rohan lotlikat

Hi Guys

I am facing a memory leak problem .. and i cant figure out whats causing it .
.. here is the code which leaks

Please help.. its making me crazy..
for(int j =1;j<=mailItemCount;j++)
{
/*CComPtr Member definition. */

if(bEndAllThreads)
_endthread();

pItems->Item((CComVariant)j,&pDisp);

{
CComQIPtr <Outlook::_MailItem> pMailItem(pDisp);

if(pMailItem == NULL )
continue;

/* get MAPIObject to query IMessage*/
pMailItem->get_MAPIOBJECT(&pUnk);
}

pUnk->QueryInterface(IID_IMessage,(void**)&Msg);


Msg->AddRef();
Msg->GetRecipientTable(0,&lpIMAPITable);
Msg->Release();
if(lpIMAPITable)
hres = lpIMAPITable->SetColumns((LPSPropTagArray)&abCol, 0);
else
return 0;

if(FAILED(hres))
return 0;

hres = lpIMAPITable->SeekRow(BOOKMARK_BEGINNING, 0, NULL);

if(FAILED(hres))
return 0;

hres = lpIMAPITable->GetRowCount(0,&ulCount);


LPSPropValue lpsProp;
ULONG uflags =0;
uflags = PR_SENDER_EMAIL_ADDRESS;
HrGetOneProp((IMAPIProp*)pUnk,uflags,(LPSPropValue FAR *)&lpsProp);
if(!(lpsProp))
continue;

bSenderEmail = lpsProp->Value.lpszA;

if (lpsProp)
MAPIFreeBuffer(lpsProp);

lpsProp = NULL;

uflags = PR_SENDER_NAME;
HrGetOneProp((IMAPIProp*)pUnk,uflags,(LPSPropValue FAR *)&lpsProp);
bSenderName = lpsProp->Value.lpszA;

pUnk->Release();

if (lpsProp)
MAPIFreeBuffer(lpsProp);

lpsProp = NULL;


CComBSTR bcontactList(bSenderEmail);
CComBSTR bnameList(bSenderName);

bcontactList += ";";

bnameList += ";";


ScanMsg = "";
ScanMsg += "Processing ";
ScanMsg += _itoa(j,buf,10);
ScanMsg += " of ";
ScanMsg += _itoa(mailItemCount,buf,10);

SetDlgItemText(IDC_STATSCANMSG,OLE2CT(ScanMsg));
StepProgressBar();
if(bSenderEmail.Length() <= 0)
{
// pMailItem.Release();
continue ;
}

LPSRestriction lpSRes = NULL;
SPropValue lpSprop;
lpSprop.ulPropTag = PR_EMAIL_ADDRESS;

MAPIAllocateBuffer(sizeof(SRestriction),(LPVOID*)&lpSRes);
lpSRes->rt = RES_CONTENT ;
lpSRes->res.resContent.ulFuzzyLevel = FL_FULLSTRING ;
lpSRes->res.resContent.ulPropTag = PR_EMAIL_ADDRESS;


for(i = m_UserEmailList.begin();i != m_UserEmailList.end();i++)
{
if(blScanMail)
break;
lpSprop.Value.lpszA = (char*)(*i).c_str();
lpSRes->res.resContent.lpProp = &lpSprop;

hres = lpIMAPITable->FindRow(lpSRes,BOOKMARK_BEGINNING,0);

MAPIFreeBuffer(lpSRes->res.resContent.lpProp);
if((_stricmp(OLE2T(bSenderEmail),(*i).c_str()) == 0) ||(hres == S_OK ))

{
blScanMail = true;
break;
}

}

MAPIFreeBuffer(lpSRes);

if(!blScanMail)
{

if(pDisp!= NULL)
pDisp->Release();

continue;
}

/* Define row set */
LPSRowSet pABCRows = NULL;

CheckInContact(OLE2T(bSenderEmail),OLE2T(bSenderName));
for(int iloop=0;iloop<(int)ulCount;iloop++)
{
hres = lpIMAPITable->QueryRows(1,NULL,&pABCRows);
if(FAILED(hres))
return 0;

if(pABCRows->cRows > 0)

if((pABCRows->aRow[0].lpProps[abPR_RECIPIENT_TYPE].Value.l == 1) ||
(pABCRows->aRow[0].lpProps[abPR_RECIPIENT_TYPE].Value.l == 2))
{
hres = pABCRows->aRow[0].lpProps[abPR_DISPLAY_NAME].Value.err;

if(SUCCEEDED(hres))
CheckInContact(pABCRows->aRow[0].lpProps[abPR_EMAIL_ADDRESS].Value.
lpszA,
pABCRows->aRow[0].lpProps[abPR_DISPLAY_NAME].Value.lpszA);

}
for (int irow = 0; irow < pABCRows->cRows; ++irow)
MAPIFreeBuffer(pABCRows->aRow[irow].lpProps);

MAPIFreeBuffer(pABCRows);

}

lpIMAPITable->Release();

blScanMail = false;

if(pDisp!= NULL){
pDisp->Release();
pDisp=NULL;

}




}
 
H

Henry Gusakovsky

See commented code.

It is better to divide this big loop into some small functions.
It is very hard to read.

for(int j =1;j<=mailItemCount;j++)
{
/*CComPtr Member definition. */

if(bEndAllThreads)
_endthread();

pItems->Item((CComVariant)j,&pDisp); //Disp +1 ref

{
CComQIPtr <Outlook::_MailItem> pMailItem(pDisp);

if(pMailItem == NULL )
continue; //Disp leak if it is not smart pointer

/* get MAPIObject to query IMessage*/
pMailItem->get_MAPIOBJECT(&pUnk); //pUnk +1 ref
}

pUnk->QueryInterface(IID_IMessage,(void**)&Msg); //Msg +1 ref


Msg->AddRef(); //Msg +1 ref
Msg->GetRecipientTable(0,&lpIMAPITable);
Msg->Release(); //Msg -1 ref
if(lpIMAPITable)
hres = lpIMAPITable->SetColumns((LPSPropTagArray)&abCol, 0);
else
return 0; //Msg ref leak if NULL

if(FAILED(hres))
return 0; //Msg ref leak if NULL

hres = lpIMAPITable->SeekRow(BOOKMARK_BEGINNING, 0, NULL);

if(FAILED(hres))
return 0; //Msg ref leak if NULL + Disp leak if it is not smart pointer

hres = lpIMAPITable->GetRowCount(0,&ulCount);


LPSPropValue lpsProp;
ULONG uflags =0;
uflags = PR_SENDER_EMAIL_ADDRESS;
HrGetOneProp((IMAPIProp*)pUnk,uflags,(LPSPropValue FAR *)&lpsProp);
if(!(lpsProp))
continue; //Msg ref leak if NULL + lpIMAPITable leaked + Disp leak if it
is not smart pointer

bSenderEmail = lpsProp->Value.lpszA;

if (lpsProp)
MAPIFreeBuffer(lpsProp);

lpsProp = NULL;

uflags = PR_SENDER_NAME;
HrGetOneProp((IMAPIProp*)pUnk,uflags,(LPSPropValue FAR *)&lpsProp);
bSenderName = lpsProp->Value.lpszA;

pUnk->Release(); //pUnk -1 ref pUnk is not used anymore so it's better to
NULL it

if (lpsProp)
MAPIFreeBuffer(lpsProp);

lpsProp = NULL;


CComBSTR bcontactList(bSenderEmail);
CComBSTR bnameList(bSenderName);

bcontactList += ";";

bnameList += ";";


ScanMsg = "";
ScanMsg += "Processing ";
ScanMsg += _itoa(j,buf,10);
ScanMsg += " of ";
ScanMsg += _itoa(mailItemCount,buf,10);

SetDlgItemText(IDC_STATSCANMSG,OLE2CT(ScanMsg));
StepProgressBar();
if(bSenderEmail.Length() <= 0)
{
// pMailItem.Release();
continue ; //Msg ref leak if NULL + lpIMAPITable leaked + Disp leak if it
is not smart pointer
}

LPSRestriction lpSRes = NULL;
SPropValue lpSprop;
lpSprop.ulPropTag = PR_EMAIL_ADDRESS;

MAPIAllocateBuffer(sizeof(SRestriction),(LPVOID*)&lpSRes);
lpSRes->rt = RES_CONTENT ;
lpSRes->res.resContent.ulFuzzyLevel = FL_FULLSTRING ;
lpSRes->res.resContent.ulPropTag = PR_EMAIL_ADDRESS;


for(i = m_UserEmailList.begin();i != m_UserEmailList.end();i++)
{
if(blScanMail)
break;
lpSprop.Value.lpszA = (char*)(*i).c_str();
lpSRes->res.resContent.lpProp = &lpSprop;

hres = lpIMAPITable->FindRow(lpSRes,BOOKMARK_BEGINNING,0);

//BIG BUG you are trying to free stack pointer
MAPIFreeBuffer(lpSRes->res.resContent.lpProp);
if((_stricmp(OLE2T(bSenderEmail),(*i).c_str()) == 0) ||(hres == S_OK ))

{
blScanMail = true;
break;
}

}

MAPIFreeBuffer(lpSRes);

if(!blScanMail)
{

if(pDisp!= NULL)
pDisp->Release(); //pDisp -1 ref it is not used NULL it

continue; //Msg ref leak if NULL + lpIMAPITable leaked
}

/* Define row set */
LPSRowSet pABCRows = NULL;

CheckInContact(OLE2T(bSenderEmail),OLE2T(bSenderName));
for(int iloop=0;iloop<(int)ulCount;iloop++)
{
hres = lpIMAPITable->QueryRows(1,NULL,&pABCRows);
if(FAILED(hres))
return 0; //Msg ref leak + lpIMAPITable leak

if(pABCRows->cRows > 0)

if((pABCRows->aRow[0].lpProps[abPR_RECIPIENT_TYPE].Value.l == 1) ||
(pABCRows->aRow[0].lpProps[abPR_RECIPIENT_TYPE].Value.l == 2))
{
hres = pABCRows->aRow[0].lpProps[abPR_DISPLAY_NAME].Value.err;

if(SUCCEEDED(hres))
CheckInContact(pABCRows->aRow[0].lpProps[abPR_EMAIL_ADDRESS].Value.
lpszA,
pABCRows->aRow[0].lpProps[abPR_DISPLAY_NAME].Value.lpszA);

}
//its better to use FreeProws there
for (int irow = 0; irow < pABCRows->cRows; ++irow)
MAPIFreeBuffer(pABCRows->aRow[irow].lpProps);

MAPIFreeBuffer(pABCRows);

}

lpIMAPITable->Release(); //lpIMAPITable -1 ref. it is not used NULL it

blScanMail = false;

if(pDisp!= NULL){
pDisp->Release(); //pDisp -1 ref it is not used
pDisp=NULL;

}

// so there is 1 ref pMSg leak

}

--
WBR
Henry


"Rohan lotlikat" пишет:
Hi Guys

I am facing a memory leak problem .. and i cant figure out whats causing it .
.. here is the code which leaks

Please help.. its making me crazy..
for(int j =1;j<=mailItemCount;j++)
{
/*CComPtr Member definition. */

if(bEndAllThreads)
_endthread();

pItems->Item((CComVariant)j,&pDisp);

{
CComQIPtr <Outlook::_MailItem> pMailItem(pDisp);

if(pMailItem == NULL )
continue;

/* get MAPIObject to query IMessage*/
pMailItem->get_MAPIOBJECT(&pUnk);
}

pUnk->QueryInterface(IID_IMessage,(void**)&Msg);


Msg->AddRef();
Msg->GetRecipientTable(0,&lpIMAPITable);
Msg->Release();
if(lpIMAPITable)
hres = lpIMAPITable->SetColumns((LPSPropTagArray)&abCol, 0);
else
return 0;

if(FAILED(hres))
return 0;

hres = lpIMAPITable->SeekRow(BOOKMARK_BEGINNING, 0, NULL);

if(FAILED(hres))
return 0;

hres = lpIMAPITable->GetRowCount(0,&ulCount);


LPSPropValue lpsProp;
ULONG uflags =0;
uflags = PR_SENDER_EMAIL_ADDRESS;
HrGetOneProp((IMAPIProp*)pUnk,uflags,(LPSPropValue FAR *)&lpsProp);
if(!(lpsProp))
continue;

bSenderEmail = lpsProp->Value.lpszA;

if (lpsProp)
MAPIFreeBuffer(lpsProp);

lpsProp = NULL;

uflags = PR_SENDER_NAME;
HrGetOneProp((IMAPIProp*)pUnk,uflags,(LPSPropValue FAR *)&lpsProp);
bSenderName = lpsProp->Value.lpszA;

pUnk->Release();

if (lpsProp)
MAPIFreeBuffer(lpsProp);

lpsProp = NULL;


CComBSTR bcontactList(bSenderEmail);
CComBSTR bnameList(bSenderName);

bcontactList += ";";

bnameList += ";";


ScanMsg = "";
ScanMsg += "Processing ";
ScanMsg += _itoa(j,buf,10);
ScanMsg += " of ";
ScanMsg += _itoa(mailItemCount,buf,10);

SetDlgItemText(IDC_STATSCANMSG,OLE2CT(ScanMsg));
StepProgressBar();
if(bSenderEmail.Length() <= 0)
{
// pMailItem.Release();
continue ;
}

LPSRestriction lpSRes = NULL;
SPropValue lpSprop;
lpSprop.ulPropTag = PR_EMAIL_ADDRESS;

MAPIAllocateBuffer(sizeof(SRestriction),(LPVOID*)&lpSRes);
lpSRes->rt = RES_CONTENT ;
lpSRes->res.resContent.ulFuzzyLevel = FL_FULLSTRING ;
lpSRes->res.resContent.ulPropTag = PR_EMAIL_ADDRESS;


for(i = m_UserEmailList.begin();i != m_UserEmailList.end();i++)
{
if(blScanMail)
break;
lpSprop.Value.lpszA = (char*)(*i).c_str();
lpSRes->res.resContent.lpProp = &lpSprop;

hres = lpIMAPITable->FindRow(lpSRes,BOOKMARK_BEGINNING,0);

MAPIFreeBuffer(lpSRes->res.resContent.lpProp);
if((_stricmp(OLE2T(bSenderEmail),(*i).c_str()) == 0) ||(hres == S_OK ))

{
blScanMail = true;
break;
}

}

MAPIFreeBuffer(lpSRes);

if(!blScanMail)
{

if(pDisp!= NULL)
pDisp->Release();

continue;
}

/* Define row set */
LPSRowSet pABCRows = NULL;

CheckInContact(OLE2T(bSenderEmail),OLE2T(bSenderName));
for(int iloop=0;iloop<(int)ulCount;iloop++)
{
hres = lpIMAPITable->QueryRows(1,NULL,&pABCRows);
if(FAILED(hres))
return 0;

if(pABCRows->cRows > 0)

if((pABCRows->aRow[0].lpProps[abPR_RECIPIENT_TYPE].Value.l == 1) ||
(pABCRows->aRow[0].lpProps[abPR_RECIPIENT_TYPE].Value.l == 2))
{
hres = pABCRows->aRow[0].lpProps[abPR_DISPLAY_NAME].Value.err;

if(SUCCEEDED(hres))
CheckInContact(pABCRows->aRow[0].lpProps[abPR_EMAIL_ADDRESS].Value.
lpszA,
pABCRows->aRow[0].lpProps[abPR_DISPLAY_NAME].Value.lpszA);

}
for (int irow = 0; irow < pABCRows->cRows; ++irow)
MAPIFreeBuffer(pABCRows->aRow[irow].lpProps);

MAPIFreeBuffer(pABCRows);

}

lpIMAPITable->Release();

blScanMail = false;

if(pDisp!= NULL){
pDisp->Release();
pDisp=NULL;

}




}
 
R

Rohan lotlikat

Hey Henry

Thanx so much.. That takes care of it. I'll keep my eyes open next time.

Thank You.

Regards
Rohan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top