Click to See Complete Forum and Search --> : memory leaks


amgturk
July 8th, 2005, 08:59 AM
hii
I need help about memory leaks.
In my project when my application finishes then memory leakage is detected.
I could detect where memory leakages occured but the leakages correspond to codes taht app wizard makes.
Can u help me.how can ı solve this ?

When ı looked lines that leakages occured they're in

IMPLEMENT_DYNCREATE(CrenkoView, CView)
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
IMPLEMENT_DYNCREATE(CrenkoDoc, CDocument)

CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CrenkoDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame
RUNTIME_CLASS(CrenkoView)); // window


Detected memory leaks!
Dumping objects ->
{2136} normal block at 0x003855B8, 25 bytes long.
Data: < .| > 10 FF 2E 7C 08 00 00 00 08 00 00 00 01 00 00 00
array_p.cpp(105) : {2130} normal block at 0x00387D98, 20 bytes long.
Data: < lT8 > 00 00 00 00 6C 54 38 00 00 00 00 00 CD CD CD CD
array_p.cpp(66) : {2129} normal block at 0x00387D58, 4 bytes long.
Data: < > 00 00 00 00
winfrm2.cpp(62) : {2128} client block at 0x00387C58, subtype c0, 192 bytes long.
a CDockBar object at $00387C58, 192 bytes long
array_p.cpp(66) : {2127} normal block at 0x00387C18, 4 bytes long.
Data: < > 00 00 00 00
winfrm2.cpp(62) : {2126} client block at 0x00387B18, subtype c0, 192 bytes long.
a CDockBar object at $00387B18, 192 bytes long
array_p.cpp(66) : {2125} normal block at 0x00387878, 4 bytes long.
Data: < > 00 00 00 00
winfrm2.cpp(62) : {2124} client block at 0x00387A18, subtype c0, 192 bytes long.
a CDockBar object at $00387A18, 192 bytes long
winfrm2.cpp(62) : {2122} client block at 0x00387918, subtype c0, 192 bytes long.
a CDockBar object at $00387918, 192 bytes long
bardock.cpp(739) : {2121} normal block at 0x00387788, 176 bytes long.
Data: < y | > D4 79 14 7C CD CD CD CD CD CD CD CD CD CD CD CD
{2120} normal block at 0x003878C0, 21 bytes long.
Data: < .| > 10 FF 2E 7C 04 00 00 00 04 00 00 00 01 00 00 00
{2119} normal block at 0x00385950, 20 bytes long.
Data: < .| > 10 FF 2E 7C 03 00 00 00 03 00 00 00 01 00 00 00
{2118} normal block at 0x003874D8, 20 bytes long.
Data: < .| > 10 FF 2E 7C 03 00 00 00 03 00 00 00 01 00 00 00
{2115} normal block at 0x003876F8, 80 bytes long.
Data: < > 00 00 00 00 00 01 00 00 00 01 00 08 00 00 00 00
plex.cpp(27) : {2112} normal block at 0x00387640, 124 bytes long.
Data: < Pv8 lT8 > 00 00 00 00 50 76 38 00 00 00 00 00 6C 54 38 00
plex.cpp(27) : {2111} normal block at 0x00387588, 124 bytes long.
Data: < `X8 > 00 00 00 00 00 00 00 00 00 00 00 00 60 58 38 00
e:\documents and settings\renko\desktop\örnek\renko\renkoview.cpp(63) : {84} client block at 0x00385860, subtype c0, 88 bytes long.
a CrenkoView object at $00385860, 88 bytes long
{80} normal block at 0x00385638, 22 bytes long.
Data: < .| > 10 FF 2E 7C 05 00 00 00 05 00 00 00 01 00 00 00
e:\documents and settings\renko\desktop\sonhazır\renko\mainfrm.cpp(16) : {77} client block at 0x00385300, subtype c0, 548 bytes long.
a CMainFrame object at $00385300, 548 bytes long
e:\documents and settings\renko\desktop\örnek\renko\renkodoc.cpp(17) : {76} client block at 0x00385268, subtype c0, 88 bytes long.
a CrenkoDoc object at $00385268, 88 bytes long
plex.cpp(27) : {75} normal block at 0x003851B0, 124 bytes long.
Data: < (P8 > 00 00 00 00 00 00 00 00 00 00 00 00 28 50 38 00
{74} client block at 0x00385150, subtype c0, 32 bytes long.
a CDocManager object at $00385150, 32 bytes long
{73} normal block at 0x003850D0, 61 bytes long.
Data: < .|, , > 10 FF 2E 7C 2C 00 00 00 2C 00 00 00 01 00 00 00
e:\documents and settings\renko\desktop\örnek\renko\renko.cpp(73) : {72} client block at 0x00385028, subtype c0, 108 bytes long.
a CSingleDocTemplate object at $00385028, 108 bytes long
{71} normal block at 0x00382EB8, 70 bytes long.
Data: < .|4 5 > 10 FF 2E 7C 34 00 00 00 35 00 00 00 01 00 00 00
{70} normal block at 0x00382E30, 70 bytes long.
Data: < .|4 5 > 10 FF 2E 7C 34 00 00 00 35 00 00 00 01 00 00 00
{69} normal block at 0x00382DB0, 63 bytes long.
Data: < .|- . > 10 FF 2E 7C 2D 00 00 00 2E 00 00 00 01 00 00 00
{68} normal block at 0x00382D28, 70 bytes long.
Data: < .|4 5 > 10 FF 2E 7C 34 00 00 00 35 00 00 00 01 00 00 00
{66} normal block at 0x00382C88, 23 bytes long.
Data: < .| > 10 FF 2E 7C 06 00 00 00 06 00 00 00 01 00 00 00
{65} normal block at 0x00382C28, 33 bytes long.
Data: < .| > 10 FF 2E 7C 10 00 00 00 10 00 00 00 01 00 00 00
{64} normal block at 0x00382BD8, 20 bytes long.
Data: < 8-8 -8 @.8 > 04 00 00 00 38 2D 38 00 C0 2D 38 00 40 2E 38 00
{63} normal block at 0x00382B78, 32 bytes long.
Data: < - | +8 8,8 > E4 2D 15 7C 04 00 00 00 DC 2B 38 00 38 2C 38 00
oleinit.cpp(85) : {60} client block at 0x00382A90, subtype c0, 68 bytes long.
a CCmdTarget object at $00382A90, 68 bytes long
{55} normal block at 0x003828F8, 2 bytes long.
Data: <c > 63 00
{48} client block at 0x003824F0, subtype c0, 64 bytes long.
a CDynLinkLibrary object at $003824F0, 64 bytes long
Object dump complete.
First-chance exception at 0x7c901e58 in renko.exe: 0xC0000005: Access violation reading location 0x00000000.
HEAP[renko.exe]: Invalid Address specified to RtlValidateHeap( 00380000, 00382918 )
Unhandled exception at 0x7c8f1230 in renko.exe: User breakpoint.
Unhandled exception at 0x1020a0d9 (msvcr70d.dll) in renko.exe: User breakpoint.
HEAP[renko.exe]: Invalid Address specified to RtlFreeHeap( 00380000, 00382918 )
Unhandled exception at 0x7c8f1230 in renko.exe: User breakpoint.
Detected memory leaks!
Dumping objects ->
{55} normal block at 0x003828F8, 2 bytes long.
Data: <c > 63 00
Object dump complete.
The thread 'Win32 Thread' (0xe9c) has exited with code 0 (0x0).
The program '[3240] renko.exe: Native' has exited with code 0 (0x0).

Jinto
July 8th, 2005, 06:34 PM
Only way to prevent leaks is to make sure they never happen.
Delete any resources you use when you are done with them.

amgturk
July 9th, 2005, 03:44 AM
yes ı know but as ı said the leaks matches with app wizard code..

Andreas Masur
July 9th, 2005, 04:37 AM
Detecting and Isolating Memory Leaks (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsdebug/html/vxcondetectingisolatingmemoryleaks.asp)
Detecting Memory Leaks in MFC (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsdebug/html/_core_detecting_memory_leaks.asp)

amgturk
July 9th, 2005, 05:13 AM
thanks mansur but ı already detected it..
actually ı found that the real leakage is at another place..

WCHAR tmp=new WCHAR(); as global.

ıf ı use delete [] tmp in destruction function ı still have leakage.

how can ı overcome?

Andreas Masur
July 9th, 2005, 05:25 AM
Well..first of all it would be

WCHAR tmp= new WCHAR;
delete tmp;

Besides that....the MFC leak detection happens to at a specified time while closing the application. Since you allocate this memory globally, the leak detection most-likely has already happened before this variable gets deleted. This is of course just an assumption since I don't know the structure of your application...

amgturk
July 9th, 2005, 05:40 AM
ok Masur maybe u can help by looking this function that is source of the problem.

ı call this function in a loop about hundred times consecutively,ı tried to delete tmp at the end of function but this time another errors happen..

WCHAR *tmp=new WCHAR; // as global

void CrenkoView::imread(int im_indx)
{
int i, j;


int redx = 0, redy = 0, redcount = 0;
int redxavg = 0, redyavg = 0;


int blackx = 0, blacky = 0, blackcount = 0;
int blackxavg = 0, blackyavg = 0;


//Sleep(50);


GdiplusStartup (&m_gdiplusToken, &gdiplusstartupinput,NULL);





swprintf(tmp, L"c:/Snapshot%d%d%d.jpg", im_indx/100, (im_indx%100)/10, im_indx%10);


Bitmap *myBitmap = Bitmap::FromFile(tmp);



Color clr;



for(i=1; i<=320; i++)
{
for(j=1; j<=240; j++)
{

// Get the color codes of the (i, j)th pixel

myBitmap->GetPixel(i,j,&clr);




if( ((int)clr.GetRed() < 245 ) && ( (int)clr.GetGreen() <255 ) && ( (int)clr.GetBlue() <240 ) )
{
if( 50<((int)clr.GetRed() ) && 200<( (int)clr.GetGreen() ) && 185<( (int)clr.GetBlue() ) )
{

redx += i;
redy += j;

redcount++;
}
}


////////////////////////////////////////////////////////////////////////////////////

if( ( (int)clr.GetRed() ) <255 && ( (int)clr.GetGreen()) <230 && ( (int)clr.GetBlue()) < 240 )
{

if( 200< ( (int)clr.GetRed() ) && 200 < ( (int)clr.GetGreen()) && 160 < ( (int)clr.GetBlue()) )
{

blackx += i;
blacky += j;

blackcount++;
}
}

}
}
// myBitmap->GetPixel(300,400,&clr);
// cout<<(int)clr.GetRed()<<endl;

// Check if such colored objects even exist!

if( redcount != 0)
{

redxavg=redx/redcount;
redyavg=redy/redcount;

}


if( blackcount != 0)
{

blackxavg=blackx/blackcount;
blackyavg=blacky/blackcount;

}

redxcoordinate[g]=redxavg;
redycoordinate[g]=redyavg;

blackxcoordinate[g]=blackxavg;
blackycoordinate[g]=blackyavg;

g++;



delete myBitmap;
//GdiplusShutdown(m_gdiplusToken);

}