PDA

View Full Version : Any reason for memory leak?



steg90
10th May 2007, 13:52
Hi,

For some reason, when checking the physical memory in task manager when the thread below is running, the memory keeps going down, I cannot understand why?



void CanRead::run()
{
m_nCount = 0;

m_bCancelled = false;
unsigned long nMsgAmount = 2;

CANDATA can[MAX];


while( !m_bCancelled )
{

unsigned long numMsgs = 0;
PASSTHRU_MSG *pRxMsg = NULL;
pRxMsg = theApp->GetMessageDetail( &numMsgs, 0 );
nMsgAmount = numMsgs; // store amount of messages we got
int index = 0;
// Read can data, store in buffer ( read say N amount and then emit? )
while ( numMsgs && !m_bCancelled )
{
QString szTemp = "";
can[index].strData = "";
unsigned int iCanId = 0;
for ( int iLoop = 0; iLoop < 4; iLoop++)
{
iCanId = (iCanId << 8 ) | pRxMsg->Data[iLoop];
}
if( pRxMsg->RxStatus & 0x100 )
can[index].strId.sprintf( "%X X", iCanId);
else
can[index].strId.sprintf( "%03X", iCanId);

for ( int iLoop = 4; (unsigned)iLoop < (pRxMsg->DataSize ); iLoop++)
{
szTemp.sprintf("%02X ", pRxMsg->Data[iLoop]);
can[index].strData += szTemp;
}

can[index].strTime.sprintf("%04d", pRxMsg->Timestamp);

numMsgs--;

m_nCount++;

} // end while ( ulNoMsgs )


msleep(1);

} // end while ( true )

}



I thought it might have been the call to GetMessageDetail, but commenting this out and the memory still leaks?

Anyone any ideas?

Kind regards,
Steve

marcel
10th May 2007, 14:05
Are you sure it is not GetMessageDetail? What if you delete pRxMsg at the end of the first while loop?

If it is not GetMessageDetail, try running without the thread, and see if the mem increases then.

Regards

steg90
10th May 2007, 15:00
Thanks Marcel,

It was me being stupid, I was continually adding an item to a listwidget which was causing it - I shouldn't have been doing that from within another thread except the main GUI one.

Regards,
Steve