For Programmers: Free Programming Magazines  


Home > Archive > Smartphone Developer Forum > January 2005 > Memory leak in LoadBitmap,BiBlt?????









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Memory leak in LoadBitmap,BiBlt?????
userforgroups@hotmail.com

2005-01-23, 4:03 pm

Strange problem I'm seeing here...

Take the following code snippet, hdc was passed into this function
after getting it from the main window's handle GetDC(hwndDialog); :


HBITMAP theBitmap,OldBitmap;
HDC pdc;



for(int i=0; i<20000; i++)
{
theBitmap = LoadBitmap(g_hInst,MAKEINTRESOURCE(IDB_B
ITMAP_BACK));
pdc = CreateCompatibleDC(hdc);
OldBitmap = (HBITMAP)SelectObject(pdc,theBitmap);
BitBlt(hdc,0,0,175,180,pdc,0,0,SRCCOPY);

SelectObject(pdc,OldBitmap);
DeleteDC(pdc);
DeleteObject(theBitmap);
}


This resets my mpx200 after some time !!! I have a program that uses
double-buffering, loads images into an HBITMAP array so we don't do
loading each time, only once during session,etc. But that was crashing
as well, so decided to strip out EVERYTHING and go back to basics with
this.
So in that situation with only doing one loadbitmap per image, I was
basically doing the CreateCompatibleDC each time, selecting it into a
DC, bitblitting it, then selecting oldbitmap into the dc, deleting dc.
But of course not deleting the bitmap until the program exits, as I
only load it once.

But as you can see, the above is pretty simple. Not sure if it happens
on any other phones, but it's very strange and very troublesome.

Another simple test is to just loop on calling SHLoadImageResource(),
loading a jpg or something into a bitmap HBITMAP, then doing a
DeleteObject on the bitmap right after. If I do that in a loop above,
without even blitting it,etc. I still reset after some time.
By the way, my bitmap is 175x180 16 bit in the resource file, and the
jpg is the same size for the SHLoadImageResource situation.
Please help!!! I have only a little hair left.

userforgroups@hotmail.com

2005-01-23, 4:03 pm

Just for a test, I downloaded and tried the sample applicaiton with
stgapibuffer
http://www.smartphonedn.com/libraries/stgapibuffer.html

for smartphone. It ran just fine, no resets after at least ten minutes
of continuous drawing. But, when I removed the sleep(5) from within
the frame drawing section, I did get it to reset just like in my
application.

I only could reproduce that once, and can't get it to reset again, even
without the sleep.

I will probably just switch to using GAPI, although I've had trouble
before on Pocket PC trying to mix GAPI with standard dialogs (for game
settings, etc.) So I ended up having to make my own graphical settings
screens without normal WIN32 API.


Any ideas on my problem would be appreciated. I'm trying to run a test
by taking my own code with the loop, and inserting a sleep as well to
see if it helps any.

Just for more background, I found this problem in my app originally by
playing it for a while (maybe 15 minutes) or so, and eventually it
would just freeze and lock the phone, had to pull the battery.

So I basically narrowed it down to the drawing stages, and then made
this tight loop to pinpoint and reproduce....that's all.

userforgroups@hotmail.com wrote:
> Strange problem I'm seeing here...
>
> Take the following code snippet, hdc was passed into this function
> after getting it from the main window's handle GetDC(hwndDialog); :
>
>
> HBITMAP theBitmap,OldBitmap;
> HDC pdc;
>
>
>
> for(int i=0; i<20000; i++)
> {
> theBitmap = LoadBitmap(g_hInst,MAKEINTRESOURCE(IDB_B
ITMAP_BACK));
> pdc = CreateCompatibleDC(hdc);
> OldBitmap = (HBITMAP)SelectObject(pdc,theBitmap);
> BitBlt(hdc,0,0,175,180,pdc,0,0,SRCCOPY);

> SelectObject(pdc,OldBitmap);
> DeleteDC(pdc);
> DeleteObject(theBitmap);
> }
>
>
> This resets my mpx200 after some time !!! I have a program that uses
> double-buffering, loads images into an HBITMAP array so we don't do
> loading each time, only once during session,etc. But that was

crashing
> as well, so decided to strip out EVERYTHING and go back to basics

with
> this.
> So in that situation with only doing one loadbitmap per image, I was
> basically doing the CreateCompatibleDC each time, selecting it into a
> DC, bitblitting it, then selecting oldbitmap into the dc, deleting

dc.
> But of course not deleting the bitmap until the program exits, as I
> only load it once.
>
> But as you can see, the above is pretty simple. Not sure if it

happens
> on any other phones, but it's very strange and very troublesome.
>
> Another simple test is to just loop on calling SHLoadImageResource(),
> loading a jpg or something into a bitmap HBITMAP, then doing a
> DeleteObject on the bitmap right after. If I do that in a loop

above,
> without even blitting it,etc. I still reset after some time.
> By the way, my bitmap is 175x180 16 bit in the resource file, and the
> jpg is the same size for the SHLoadImageResource situation.
> Please help!!! I have only a little hair left.


userforgroups@hotmail.com

2005-01-23, 4:03 pm

http://www.smartphonedn.com/libraries/stgapibuffer.html

I have another update on this gapi sample application. I ran it again,
without any changes, and let it run longer than 10 minutes.... at
around 15 minutes of running, my device reset!

This should be easy to reproduce, and it appears to be a problem with
both GAPI and normaly GDI. I doubt both of our programs have the same
problem.

Anyone else can try the sample application at the above link on their
phone?



userforgroups@hotmail.com wrote:
> Just for a test, I downloaded and tried the sample applicaiton with
> stgapibuffer
> http://www.smartphonedn.com/libraries/stgapibuffer.html
>
> for smartphone. It ran just fine, no resets after at least ten

minutes
> of continuous drawing. But, when I removed the sleep(5) from within
> the frame drawing section, I did get it to reset just like in my
> application.
>
> I only could reproduce that once, and can't get it to reset again,

even
> without the sleep.
>
> I will probably just switch to using GAPI, although I've had trouble
> before on Pocket PC trying to mix GAPI with standard dialogs (for

game
> settings, etc.) So I ended up having to make my own graphical

settings
> screens without normal WIN32 API.
>
>
> Any ideas on my problem would be appreciated. I'm trying to run a

test
> by taking my own code with the loop, and inserting a sleep as well to
> see if it helps any.
>
> Just for more background, I found this problem in my app originally

by[color=darkred]
> playing it for a while (maybe 15 minutes) or so, and eventually it
> would just freeze and lock the phone, had to pull the battery.
>
> So I basically narrowed it down to the drawing stages, and then made
> this tight loop to pinpoint and reproduce....that's all.
>
> userforgroups@hotmail.com wrote:
uses[color=darkred]
> crashing
> with
was[color=darkred]
a[color=darkred]
> dc.
> happens
SHLoadImageResource(),[color=darkred]
> above,
the[color=darkred]

userforgroups@hotmail.com

2005-01-23, 4:03 pm

Have another update on this...

I tried the same gapi sample program on a Samsung i600 (running
Smartphone OS 2003), and works just fine. No resetting. I've been
running it for about 30 minutes straight now, no problems. The same
program on my motorola mpx200 resets at around 12 minutes.

Unfortunately, I don't have another Smartphone 2002 phone around, so I
can't see if it is an OS thing, or an OEM thing.

Anyone care to shed some light on this?

userforgroups@hotmail.com wrote:
> http://www.smartphonedn.com/libraries/stgapibuffer.html
>
> I have another update on this gapi sample application. I ran it

again,
> without any changes, and let it run longer than 10 minutes.... at
> around 15 minutes of running, my device reset!
>
> This should be easy to reproduce, and it appears to be a problem with
> both GAPI and normaly GDI. I doubt both of our programs have the

same[color=darkred]
> problem.
>
> Anyone else can try the sample application at the above link on their
> phone?
>
>
>
> userforgroups@hotmail.com wrote:
> minutes
within[color=darkred]
> even
trouble[color=darkred]
> game
> settings
> test
to[color=darkred]
> by
made[color=darkred]
function[color=darkred]
:[color=darkred]
> uses
do[color=darkred]
> was
into[color=darkred]
> a
deleting[color=darkred]
I[color=darkred]
> SHLoadImageResource(),
> the

leguao

2005-01-25, 4:02 pm

I would at least check whether either
DeleteDC(pdc);
or
DeleteObject(theBitmap);
fail by checking the return code. At least that way you'll be sure that
your code is OK.

Other than that I'm not sure that getting a DC by GetDC and then using it
constantly until your system crashes is wise. I suppose it would be better
to within the loop do the GetDC and do a ReleaseDC after you're done..

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com