Memory Leak
-
I use CImage class for working with bitmaps and noticed memory leak and can't understand the problem. When i'm trying to change the content of bitmap by accessing the bytes - in different cases i see or not see memory leaks, however the code is same! Here is the code:
UINT __cdecl CGNTGDALDlg::GNTClassificationFunction( LPVOID pParam ) { ThreadStruct* pStruct = (ThreadStruct*)pParam; int n = pStruct->pDlg->m_Image.GetHeight(); int m = pStruct->pDlg->m_Image.GetWidth(); int LineLength = pStruct->pDlg->m_Image.GetPitch(); unsigned char* Bitmap = (unsigned char*)pStruct->pDlg->m_Image.GetBits(); Bitmap+=LineLength*pStruct->Offset; if(pStruct->pDlg->m_floatingWindowSize==0) { __declspec(align(16)) float* pInputs =(float*) _aligned_malloc(pStruct->pDlg->m_poDataset->GetRasterCount()*sizeof(float),16); for(int i =pStruct->Offset;iEnd;i++) { for(int j = abs(LineLength%m),l=0;jpDlg->m_poDataset->GetRasterCount();k++) { pInputs[k] = pStruct->pDlg->m_pImageMatrix[k][i][m-l-1]; Norm+=pInputs[k]; } Norm = sqrt(Norm); for(int k = 0;kpDlg->m_poDataset->GetRasterCount();k++) pInputs[k]/=Norm; EnterCriticalSection(&pStruct->pDlg->m_CriticalSection); unsigned int value = pStruct->pDlg->m_pGNTNet->Propagate(pInputs,distance,&pNeuron); LeaveCriticalSection(&pStruct->pDlg->m_CriticalSection); *(Bitmap-LineLength-j-1) = GetRValue(value);//eto krasnii *(Bitmap-LineLength-j-2) = GetGValue(value);//eto zelenii *(Bitmap-LineLength-j-3) = GetBValue(value);//eto sinii } Bitmap+=LineLength; } _aligned_free(pInputs); } else { __declspec(align(16)) float* pInputs =(float*) _aligned_malloc(pStruct->pDlg->m_floatingWindowSize*pStruct->pDlg->m_floatingWindowSize*pStruct->pDlg->m_poDataset->GetRasterCount()*sizeof(float),16); for(int i =pStruct->Offset;iEnd;i++) { if(ipDlg->m_poDataset->GetRasterYSize()-pStruct->pDlg->m_floatingWindowSize) for(int j = abs(LineLength%m),l=0;jpDlg->m_poDataset->GetRasterXSize()-pStruct->pDlg->m_floatingWindowSize) { double distance; CGNTNeuron* pNeuron; int ll = 0; double Norm = 0; for(int k = 0;kpDlg->m_poDataset->GetRasterCount();k++) for(int ii
-
I use CImage class for working with bitmaps and noticed memory leak and can't understand the problem. When i'm trying to change the content of bitmap by accessing the bytes - in different cases i see or not see memory leaks, however the code is same! Here is the code:
UINT __cdecl CGNTGDALDlg::GNTClassificationFunction( LPVOID pParam ) { ThreadStruct* pStruct = (ThreadStruct*)pParam; int n = pStruct->pDlg->m_Image.GetHeight(); int m = pStruct->pDlg->m_Image.GetWidth(); int LineLength = pStruct->pDlg->m_Image.GetPitch(); unsigned char* Bitmap = (unsigned char*)pStruct->pDlg->m_Image.GetBits(); Bitmap+=LineLength*pStruct->Offset; if(pStruct->pDlg->m_floatingWindowSize==0) { __declspec(align(16)) float* pInputs =(float*) _aligned_malloc(pStruct->pDlg->m_poDataset->GetRasterCount()*sizeof(float),16); for(int i =pStruct->Offset;iEnd;i++) { for(int j = abs(LineLength%m),l=0;jpDlg->m_poDataset->GetRasterCount();k++) { pInputs[k] = pStruct->pDlg->m_pImageMatrix[k][i][m-l-1]; Norm+=pInputs[k]; } Norm = sqrt(Norm); for(int k = 0;kpDlg->m_poDataset->GetRasterCount();k++) pInputs[k]/=Norm; EnterCriticalSection(&pStruct->pDlg->m_CriticalSection); unsigned int value = pStruct->pDlg->m_pGNTNet->Propagate(pInputs,distance,&pNeuron); LeaveCriticalSection(&pStruct->pDlg->m_CriticalSection); *(Bitmap-LineLength-j-1) = GetRValue(value);//eto krasnii *(Bitmap-LineLength-j-2) = GetGValue(value);//eto zelenii *(Bitmap-LineLength-j-3) = GetBValue(value);//eto sinii } Bitmap+=LineLength; } _aligned_free(pInputs); } else { __declspec(align(16)) float* pInputs =(float*) _aligned_malloc(pStruct->pDlg->m_floatingWindowSize*pStruct->pDlg->m_floatingWindowSize*pStruct->pDlg->m_poDataset->GetRasterCount()*sizeof(float),16); for(int i =pStruct->Offset;iEnd;i++) { if(ipDlg->m_poDataset->GetRasterYSize()-pStruct->pDlg->m_floatingWindowSize) for(int j = abs(LineLength%m),l=0;jpDlg->m_poDataset->GetRasterXSize()-pStruct->pDlg->m_floatingWindowSize) { double distance; CGNTNeuron* pNeuron; int ll = 0; double Norm = 0; for(int k = 0;kpDlg->m_poDataset->GetRasterCount();k++) for(int ii
I'll try to help but I can't read your posted code. Can you wrap it in "pre" tags instead of "code" tags? How do you know there's a leak? How are you checking? Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
I'll try to help but I can't read your posted code. Can you wrap it in "pre" tags instead of "code" tags? How do you know there's a leak? How are you checking? Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
Well, i looked in the task manager - when this process is running the momory grows up. Here is code in pre tags
UINT __cdecl CGNTGDALDlg::GNTClassificationFunction( LPVOID pParam )
{
ThreadStruct* pStruct = (ThreadStruct*)pParam;
int n = pStruct->pDlg->m_Image.GetHeight();
int m = pStruct->pDlg->m_Image.GetWidth();
int LineLength = pStruct->pDlg->m_Image.GetPitch();
unsigned char* Bitmap = (unsigned char*)pStruct->pDlg->m_Image.GetBits();
Bitmap+=LineLength*pStruct->Offset;
if(pStruct->pDlg->m_floatingWindowSize==0)
{
__declspec(align(16)) float* pInputs =(float*) _aligned_malloc(pStruct->pDlg->m_poDataset->GetRasterCount()*sizeof(float),16);
for(int i =pStruct->Offset;iEnd;i++)
{
for(int j = abs(LineLength%m),l=0;jpDlg->m_poDataset->GetRasterCount();k++)
{
pInputs[k] = pStruct->pDlg->m_pImageMatrix[k][i][m-l-1];
Norm+=pInputs[k];
}
Norm = sqrt(Norm);
for(int k = 0;kpDlg->m_poDataset->GetRasterCount();k++)
pInputs[k]/=Norm;
EnterCriticalSection(&pStruct->pDlg->m_CriticalSection);
unsigned int value = pStruct->pDlg->m_pGNTNet->Propagate(pInputs,distance,&pNeuron);
LeaveCriticalSection(&pStruct->pDlg->m_CriticalSection);\*(Bitmap-LineLength-j-1) = GetRValue(value);//eto krasnii \*(Bitmap-LineLength-j-2) = GetGValue(value);//eto zelenii \*(Bitmap-LineLength-j-3) = GetBValue(value);//eto sinii } Bitmap+=LineLength; } \_aligned\_free(pInputs); } else { \_\_declspec(align(16)) float\* pInputs =(float\*) \_aligned\_malloc(pStruct->pDlg->m\_floatingWindowSize\*pStruct->pDlg->m\_floatingWindowSize\*pStruct->pDlg->m\_poDataset->GetRasterCount()\*sizeof(float),16); for(int i =pStruct->Offset;iEnd;i++) { if(ipDlg->m\_poDataset->GetRasterYSize()-pStruct->pDlg->m\_floatingWindowSize) for(int j = abs(LineLength%m),l=0;jpDlg->m\_poDataset->GetRasterXSize()-pStruct->pDlg->m\_floatingWindowSize) { double distance; CGNTNeuron\* pNeuron; int ll = 0; double Norm = 0; for(int k = 0;kpDlg->m\_poDataset->GetRasterCount();k++) for(int ii = i;iipDlg->m\_floatingWindowSize;ii++) for(int jj = l;jjpDlg->m\_floatingWindowSize;jj++) { pInputs\[ll++\] = pS
-
Well, i looked in the task manager - when this process is running the momory grows up. Here is code in pre tags
UINT __cdecl CGNTGDALDlg::GNTClassificationFunction( LPVOID pParam )
{
ThreadStruct* pStruct = (ThreadStruct*)pParam;
int n = pStruct->pDlg->m_Image.GetHeight();
int m = pStruct->pDlg->m_Image.GetWidth();
int LineLength = pStruct->pDlg->m_Image.GetPitch();
unsigned char* Bitmap = (unsigned char*)pStruct->pDlg->m_Image.GetBits();
Bitmap+=LineLength*pStruct->Offset;
if(pStruct->pDlg->m_floatingWindowSize==0)
{
__declspec(align(16)) float* pInputs =(float*) _aligned_malloc(pStruct->pDlg->m_poDataset->GetRasterCount()*sizeof(float),16);
for(int i =pStruct->Offset;iEnd;i++)
{
for(int j = abs(LineLength%m),l=0;jpDlg->m_poDataset->GetRasterCount();k++)
{
pInputs[k] = pStruct->pDlg->m_pImageMatrix[k][i][m-l-1];
Norm+=pInputs[k];
}
Norm = sqrt(Norm);
for(int k = 0;kpDlg->m_poDataset->GetRasterCount();k++)
pInputs[k]/=Norm;
EnterCriticalSection(&pStruct->pDlg->m_CriticalSection);
unsigned int value = pStruct->pDlg->m_pGNTNet->Propagate(pInputs,distance,&pNeuron);
LeaveCriticalSection(&pStruct->pDlg->m_CriticalSection);\*(Bitmap-LineLength-j-1) = GetRValue(value);//eto krasnii \*(Bitmap-LineLength-j-2) = GetGValue(value);//eto zelenii \*(Bitmap-LineLength-j-3) = GetBValue(value);//eto sinii } Bitmap+=LineLength; } \_aligned\_free(pInputs); } else { \_\_declspec(align(16)) float\* pInputs =(float\*) \_aligned\_malloc(pStruct->pDlg->m\_floatingWindowSize\*pStruct->pDlg->m\_floatingWindowSize\*pStruct->pDlg->m\_poDataset->GetRasterCount()\*sizeof(float),16); for(int i =pStruct->Offset;iEnd;i++) { if(ipDlg->m\_poDataset->GetRasterYSize()-pStruct->pDlg->m\_floatingWindowSize) for(int j = abs(LineLength%m),l=0;jpDlg->m\_poDataset->GetRasterXSize()-pStruct->pDlg->m\_floatingWindowSize) { double distance; CGNTNeuron\* pNeuron; int ll = 0; double Norm = 0; for(int k = 0;kpDlg->m\_poDataset->GetRasterCount();k++) for(int ii = i;iipDlg->m\_floatingWindowSize;ii++) for(int jj = l;jjpDlg->m\_floatingWindowSize;jj++) { pInputs\[ll++\] = pS
As long as you are freeing memory you allocate you should be fine. The task manager isn't always a great leak detector. Memory allocated for your process may be marked as available to the application but not freed until it's necessary - for performance. Trust your code :) The debug C runtime (CRT) will show leaks at runtime for malloc, _aligned_malloc, new, etc. Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
As long as you are freeing memory you allocate you should be fine. The task manager isn't always a great leak detector. Memory allocated for your process may be marked as available to the application but not freed until it's necessary - for performance. Trust your code :) The debug C runtime (CRT) will show leaks at runtime for malloc, _aligned_malloc, new, etc. Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
Ok! Thank You very much for participating.