Are there any known memory leaks with StretchBlt and BitBlt

Posted on 2006-03-24
Last Modified: 2013-11-20
I have a pretty straightforward bit of code that that uses two CDCs to paste an HBITMAP into a larger HBITMAP at a specified rect. The code works, but I was noticing a memory leak as the stitching was happening.  

When I first create the larger HBITMAP, I see the Virtual Memory usage in my task manager increase by the size of the bitmap, as I would expect. Correct me if I'm wrong, but when I then bit blit data into an area of that large HBITMAP, the "Mem Usage" column in the task manager should not necessarilly increase, correct?

When I commented out the StretchBlt line and let everything run, both the VM Size and Mem Usage values in the task manager remained the same. Allowing the StretchBlt to happen, and the Mem Usage column kept increasing, while VM Size stayed the same.

I use StretchBlt because my code is used both when I'm pasting a smaller image into a bigger image and when I"m pasting a bigger image into a smaller image, but in this case, I tried using BitBlt as well, and saw the same increase in Mem Usage.

Is there a problem with my code?   I don't understand why performing a StretchBlt (or BitBlt) would cause the memory used to increase.


CRect pasteRect;
GetRectAtIndex(a_iIndex, &pasteRect); // returns a 1000x1000 rect

CDC memDCImage, memDCThumbs;



HBITMAP hbmOldImage = (HBITMAP)memDCImage.SelectObject(a_hbmImage);
HBITMAP hbmOldThumb      = (HBITMAP)memDCThumbs.SelectObject(m_hbmThumbnailSurface);      
memDCThumbs.StretchBlt(pasteRect.left,, pasteRect.Width(), pasteRect.Height(), &memDCImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);



Question by:PMH4514
    LVL 49

    Expert Comment

    What APIs are you using to create the HBITMAP.  It is perfectly possible that those steps just cause the system to allocate memory and that no physical memory would be accessed until such time as you "touch" the data.  

    I'll bet that you can see a similar jump in physical memory usage if you even use SetPixel -- anything that actually changes a value in the allocated memory -- including directly accessing it via its lpBits or SetDIBits() ... It might also jump if you use GetDIBits() since I'd assume that would cause the memory are to be locksed and thus forcing a change from virtual to physical.

    -- Dan

    Author Comment

    Dan -
    I create them using CreateDIBSection

    I understand what you're saying, but in some instances, when the HBITMAP is created, I may see an increase in used Virtual Memory by upwards or 400MB depending on the size of the large image into which my sub-images are stitchec. My System Ram usage may start off around, oh say 20MB, but as the map is built, the system comes to a hault as the Mem Usage goes approaches the amount of physical RAM on the system.   If ~500MB of virtual ram were allocated for an HBITMAP that required 500MB, I don't understand why filling that space up would cause such a dramatic increase in used physical ram.

    And if that is normal operation, how do I alleviate the problem?
    LVL 49

    Accepted Solution

    As you "touch" various parts of the address space, the system will have to make "real" RAM available.  Since it the most recently used, it is high priority and the system will have to start swapping out parts of other programs to disk... At some point, just dropping down a menu will cause the system to have to read code and data from the disk...

    Another thought: The amount of VM actualized to real memory may vary depending upon the location in the bitmap.  For instance, touching the lower right corner might be much more "expensive" than touching the upper left.  

    It might also depend on the shape the the BMP.   For instance, one that is 16,000 pixels wide consumes 48K of RAM per scanline.  I imagine touching just the leftmost pixel of each line would cause the entire line to be actualized into RAM.  So a "tall and narrow" blit to a really wide Bitmap could be very expensive.

    The first thing I'd try is to use the FileMapping technique in the CreateDIBSection call.  It seems to me that there is a good chance the system will be quicker to page out parts of the huge BMP allocation in that case (it's the very reason for that feature).  It might use "sparse matrix" techniques to avoid gobbling vast tracts of disk and RAM space.

    The next try would be much harder... you would need to find a way of avoiding ever having to use the whole BMP at once... that means manually seeking into the right parts of the file to locate the right pixel data -- as in http:/MFC/Q_20312390.html -- and read and write it manually (i.e., using techniques other than Bitblit or StretchBlit).  Though, it should be possible to StrtchBlit to smaller, temporary bitmaps formatted correctly and then transfer the resulting data to disk.

    -- Dan
    LVL 43

    Expert Comment

    Minor point.
    A memory leak is typically assigning a block of memory with new, but later not freeing it with delete.  In DEBUG mode the IDE will give warnings when the app is closed.

    Author Comment

    right Andy, I did hesitate to use the term memory leak here, because the IDE wasn't giving such warnings. Perhaps what I'm seeing is in fact normal functionality as Dan is implying.
    LVL 49

    Expert Comment

    It's a longshot, but it might be worth testing to see if the HALFTONE adjustment logic for your device driver is causing the extra memory to be allocated "behind the scenes".  Just comment out the line...
    ans see if the behavior changes.

    Author Comment

    yah no effect. I had tried various blt modes, including no specification, still see the same thing.

    'course this may become moot with the code from my other question you've been helping with

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
    Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now