seems that i've a resource leak somewhere..could someone just quickly check my code please?


invoke CreateCompatibleDC,myDC
mov mapbitdc,eax

on WM_COMMAND (pressing a button):

invoke RtlZeroMemory,addr mappath,255
mov ofn.lStructSize,SIZEOF ofn
push hWin
pop ofn.hWndOwner
push hInstance
pop ofn.hInstance
mov ofn.lpstrFilter, OFFSET FilterString
mov ofn.lpstrFile, OFFSET mappath
mov ofn.nMaxFile,255
mov ofn.Flags,OFN_LONGNAMES or\
invoke GetOpenFileName, ADDR ofn
.if eax!=0
invoke DeleteObject,mapbit
invoke BitmapFromFile, ADDR mappath
.if eax==0
invoke GetLastError
invoke FormatMessage,FORMAT_MESSAGE_FROM_SYSTEM,0,eax,0,addr errormessage,50,0
invoke MessageBox,hWin,addr errormessage,addr szDisplayName,0
mov mapbit,eax
invoke SelectObject,mapbitdc,mapbit
INVOKE FillRect, backbuffer, ADDR screenrect, whitebrush
invoke BitBlt,backbuffer,0,0,screenrect.right,screenrect.bottom,mapbitdc,0,0,SRCCOPY
invoke paintgirder
invoke newgame

I think there must be a resource leak, because: when i try to select a map (a picture) the first time, nothing is BitBlted on the screen. if i try a second and a third time with the same file, it works. but after 4 or 5 times of selecting a map, the map isn't BitBlted on my window anymore, but directly on the desktop... seems that there's something wrong...

thanks in advance..

Posted on 2002-06-09 04:42:38 by NOP-erator
ReleaseDC ?
Posted on 2002-06-09 06:34:42 by bazik
ReleaseDC is obviously only used to release the main window's dc. to release some other dc that is created by CreateDC i should use DeleteDC. but in my WM_DESTROY message handler, i delete all the stuff..but there's something that is not released or freed during runtime, everytime i load another picture in the dc..(?)

Posted on 2002-06-09 06:46:09 by NOP-erator
I am not very experienced with the GDI stuff. But I had a problem with my GDI routine 2h ago, because I forgot ReleaseDC :)
Posted on 2002-06-09 08:41:37 by bazik
Reading your code I don't see any leaks.

But from your description of the problem it doesn't sound like a leak, at least not one associated with bitmap.

If you want you could e-mail me the source and I'll have a look through it tonight when I get home from work. The problem sounds strange. :confused:
Posted on 2002-06-09 13:06:50 by Eóin
invoke BitmapFromFile, ADDR mappath

.if eax==0
invoke GetLastError
invoke FormatMessage, FORMAT_MESSAGE_FROM_SYSTEM, 0, eax, 0, addr errormessage, 50, 0
invoke MessageBox, hWin, addr errormessage, addr szDisplayName, 0
mov mapbit,eax
invoke SelectObject,mapbitdc,mapbit
INVOKE FillRect, backbuffer, ADDR screenrect, whitebrush
invoke BitBlt,backbuffer,0,0,screenrect.right,screenrect.bottom,mapbitdc,0,0,SRCCOPY
invoke paintgirder
invoke newgame[/color]
My question is: Do continue to allow to execute the red part even if the returned value of BitmapFromFile produces an error? Just my guess??? if not then try adding an .else statement for the red part.
Posted on 2002-06-09 13:43:03 by stryker
stryker: there is no error because BitmapFromFile returns zero if there is any error, so i would see a messagebox, of course.. there is no error with BitmapFromFile

E?in: ok, thank you very much! I'll send it to you...
..just noticed that i don't have your e-mail address..
Posted on 2002-06-09 13:48:13 by NOP-erator
Oh! there's probably something wrong on the paintgirder or newgame procedures. Try adding more error checking on other API calls.
Posted on 2002-06-09 13:50:19 by stryker
here is the source in a zip file..
Posted on 2002-06-09 13:52:54 by NOP-erator
I don't have the Image.lib and Image.inc you specified on your program but try adding this after BitBlt invoke ShowWindow,hwnd,SW_SHOW - just my guess.
Posted on 2002-06-09 14:10:39 by stryker
no, that doesn't change anything..
Posted on 2002-06-09 14:12:30 by NOP-erator
invoke SelectObject,mapbitdc,mapbit
mov oldobject,eax

when you finish your job
invoke SelectObject,mapbitdc,oldobject

That is the only thing I can think of.

Posted on 2002-06-09 15:39:19 by LaptoniC
This is very interesting.

The first problem, regarding nothing happenig when you first load a map I'm pretty sure is simply that the app recieves no WM_PAINT message, so you either have to click LOS (go?) which does a BitBlt to myDC, or cause a WM_PAINT message to be sent by covering the window with another.

Easily solved by sticking an InvalidateRect at the end of code execute by the MAP button.

But after this is gets very strange. When you say "if i try a second and a third time with the same file, it works." I take it you mean quiting the program and restarting, as after I ran the program a couple of times it did indeed start to display the map after I loaded it. :confused: I don't know really but perhaps there is some reason behind the scenes in windows and it starts sending WM_PAINT message after an OpenFile dialog box closes or something. I don't think thats much of a problem.

But the image being displayed on the desktop could be a serious problem. Unfortunatly I can't seem to repeat it, but its almost definitly a DC problem (thats no help I know). Things to try are

1) Include CS_OWNDC as a class style along with CS_HREDRAW, CS_VREDRAW & CS_BYTEALIGNWINDOW.
2) Put error checking everywhere a DC handle is stored. Ie don't assume GetDC or CreateCompatibleDC work every time.

And finally what exactly is being displayed on the desktop, just the image or the image and grid.
Posted on 2002-06-09 17:17:05 by Eóin
Oh yeah, just remembered. If you call GetDC with 0 as the window handle then anything you draw to the DC returned ends up on the Desktop.

I don't know if 0 is simply the handle of the desktop or its something else happening but that could be related to your second problem.
Posted on 2002-06-10 03:46:59 by Eóin
Creating the dc must be deleted after it does not use any more.
The API is DeleteDC()
Posted on 2002-06-10 03:57:17 by yoursguideline
yoursguideline: i delete all the bitmaps and dc on WM_DESTROY
E?in: ok, that the map isn't displayed the first time was actually a WM_PAINT message thing..this works now, thanks.

to repeat the other serious problem (painting directly on the desktop) you don't need to restart the program, but loading a map 3 to 4 times, one after the other WITHOUT restarting the program. that means: chose a map, chose one again, and again, and again...

this should work then.. (hehe, or not work :tongue: )

thanks for helping..hope we (better: you) can solve the last problem, too...

Posted on 2002-06-10 13:22:15 by NOP-erator
to find memory leaks
check this prog http://www.automatedqa.com/
called "memproof"
Posted on 2002-06-10 17:03:36 by eko
thank you for the link, but i couldn't solve my problem with the tool..sorry...
Posted on 2002-06-11 11:34:00 by NOP-erator
NOP, I've finally witinessed the problem. It wouldn't manifest itself on my XP box, but when I managed to get the program onto my 98 one then sure enough it happened as you described.

Itsv ery hard for me to solve it as I can't compile on the WIn98 box, beside I dont have the image lib & inc file.

However, it doesn't mean I've given up, attached are three file each with a slight modification to your program. Compile and run them, hopefully one of the first two will fix it. :cool:

If not then compile and run the third. The change I made to it are merely to provide feedback on what's happening, could you tell me which of the test messages display and where they display when A) the map shows properly, B) the map is drawn on the desktop.
Posted on 2002-06-11 12:08:50 by Eóin

ok, here is the report: formelv1.asm works, formelv2.asm works as well, and formelv3.asm seemed to work sometimes :)

formelv1: works without any problems
formelv2: works without any problems
formelv3: this version did sometimes work, sometimes there was only one message "testing" and sometimes two times. when it was there two times and the map was displayed properly, both messages were in the left upper corner of the window. when the map was displayed wrongly, one was painted with the map on the desktop and the other seemed to be placed correctly on the window.

but, would you please explain, why version 1 and 2 work now? i don't get where the problem is....

THANK YOU!!! *hug* ;) :tongue:

Posted on 2002-06-11 12:31:00 by NOP-erator