473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

GDI+ DrawImage leaks memory but I don't know why

I'm using Dev C++ to do a simple Win32 app on a Windows Embedded Standard platform and am having trouble understanding why this code has a memory leak. This is the WM_PAINT message handler for the window procedure. The 'initialized' variable is a global Boolean that's initialized to false, and then set true after the WM_CREATE message is handled.

case WM_PAINT:{
if(initialized){
PAINTSTRUCT ps;
hDC = BeginPaint(&ps);
static int slide = 0;
assert(slide >= 0);
assert(slide <= 28);
Image * pSlide = pSlides[slide];
Graphics graphics(hDC);
graphics.DrawImage(pSlide, 0, 0);
if(++slide > 28){
slide = 0;
}
}
break;
}

Prior to the WM_PAINT message being handled the pSlides[] array is globally declared and filled like this.

Image * pSlides[29];

case WM_CREATE:{
wchar_t filename[] = L"slides\\slide01.png";
for(int i=0; i<29; i++){
swprintf(filename,L"slides\\slide%02d.png", i);
pSlides[i] = new Image(filename);
assert(pSlides[i]);
}
initialized = true;
break;
}

Using Windows sysinternals process explorer, I can see that every time the WM_PAINT message draws an image (on top of the prior image) memory is leaked.

If I comment out the call to graphics.DrawImage, the memory leak goes away.

This slide show updates the screen once every second and runs for hours on end, until, I think, too much memory gets leaked at which point it appears to hang/crash.

Can anyone tell me how I can fix this memory leak?

Thanks!
Sep 9 '15 #1
5 2607
If I changed my graphics assets to use BMP instead of PNG files, so the DrawImage function is drawing Bitmaps instead of Images, the memory leak goes away. I don't see how or why that could be, but I did observe it.

Comments...?
Sep 9 '15 #2
Now I'm really confused. I created my Bitmap objects like this

Expand|Select|Wrap|Line Numbers
  1. Bitmap * pSlide01 = new Bitmap(L"slides\\slide_01.bmp");
  2. Bitmap * pSlide02 = new Bitmap(L"slides\\slide_02.bmp");
  3. Bitmap * pSlide03 = new Bitmap(L"slides\\slide_03.bmp");
  4. ...
  5. Bitmap * pSlide29 = new Bitmap(L"slides\\slide_29.bmp");
  6.  
and drew them like this

Expand|Select|Wrap|Line Numbers
  1. switch(slide){
  2.   case 1:  graphics.DrawImage(pSlide01,0,0); break;
  3.   case 2:  graphics.DrawImage(pSlide02,0,0); break;
  4.   case 3:  graphics.DrawImage(pSlide03,0,0); break;
  5.   ...
  6.   case 29: graphics.DrawImage(pSlide29,0,0); break;
  7. }
  8.  
and it works (no memory leaks). But... if I do this...

Expand|Select|Wrap|Line Numbers
  1. Bitmap * pSlides[29];
  2.  
  3. pSlides[0]  = pSlide01;
  4. pSlides[1]  = pSlide02;
  5. pSlides[2]  = pSlide03;
  6. ...
  7. pSlides[28] = pSlide29;
  8.  
  9. graphics.DrawImage(pSlides[slide],0,0);
  10.  
they're drawn properly but the memory leak comes back.

Any ideas why this might be?

Confuzed...
Sep 10 '15 #3
weaknessforcats
9,208 Expert Mod 8TB
Your bitmaps are created on the heap using the new operator. Where is it you delete these allocations??

Deleting the array does not delete the elements.
Sep 10 '15 #4
The bitmaps stay loaded for the duration of the app. They're for a slide show that repeats forever, until the app exits. Then they're deleted. They're loaded when the app window is created, and deleted with the app window is closed. They're not loaded over and over... they're only loaded once.
Sep 10 '15 #5
I have another update... if I speed up the timer and change the slides 10 times per second, the memory leak occurs, but much more slowly, even if I do it without using the array of pointers to the bitmaps. However, if I comment out the call to DrawImage, the leak goes away forever.
Sep 10 '15 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Farooq Khan | last post by:
hi, my development team has been assigned a project that involves writing our own Web service. i happen to be a C++ programmer, new to C#/ASP.net. i dont know where to begin with......any...
2
by: Pavel Novotny | last post by:
H I'm trying to import a | seperated text from string variable which represents a tabl with several fields, eg 6124079|PRIRUBA 11 DN250 PN6;CSN131160.0;11369.1;CSN131005.50|KS|11,100000|3437,...
1
by: Rudy Meijer | last post by:
Hello, I made a class which read a key from the registry. This class is called every second in a timer event. Everything works fine but the class leaks memory. about 4Kb/s taskmanager. ...
5
by: LinuxGuy | last post by:
Can someone please tell if following code leaks memory void someFunction() { char MyArray; memset(MyArray,0,512); }
0
by: arunavlp | last post by:
hi, I am new to .net , i am using VB.Net (Web form). I dont know how to pass parameters to stored function. Regards, Arun.S
2
by: hojjatnikan | last post by:
please help me this code 62EH&5gx0wiqoQFw is this name ( Belux) but i dont know how convert it i dont know the algorithm of this code plead help me
2
by: Tiruak | last post by:
Hi there. Thanks in advance for the people reading and trying to help. I'm very begginer using flash and action script, and I tryed to do this one navigation menu. Since I dont have experience...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.