Re: Free up allocated memory

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



> Hi, I have two questions of freeing up memory, I hope someone can help me:
>
> 1. After using BitBlt() function to capture the display content to picture
> box, is it necessary to free up the memory? The follwoing is the code:
>
> Picture1.AutoRedraw = True
> ret = BitBlt(Picture1.hdc, 0, 0, Width, Height, hdcmem, 0, 0, _
> SRCCOPY)
> Picture1.AutoRedraw = False
> Picture1.Refresh

Depending on where mdcmem came from, that (and it's associated GDI objects, Bitmaps/Pens/Brushes and so on) may need to be freed but
it really depends on how you get them.

> The picture box contain the captured content, however, it can not be
> recognize by the picture box, the PaintPicture method returns error said that
> the picture is invalid. Does it mean that there is no linkage between the
> bitamp and PictureBox. I mean VB will not destroy the bitmap after PictureBox
> destroyed. Is it need to use the DeleteObject function, how to do this? Also,
> is it need to use SelectObject function?

A picture box control actually has two pictures, one stored in the .Image property which is where you draw to with BitBlt() and so
on, and one stored in the .Picture property which is the background image if you want to think of it in that way. You can either
send PaintPicture() your .Image property or sync. the two properties with:

'***
Set Picture1.Picture = Picture1.Image
'***

This doesn't actually allocate any additional memory, it just makes both object references point to the same picture object. This
object is reference counted and as such will be destroyed when it's not being used any more (either because a new picture was given
to the control, or the application was unloaded which killed all the child controls.)

> 2.There is an alternative LoadPicture function written in C and compiled to
> a dll file. In VB, I can call the dll file from the following code:
<code snipped>
> The function has called the GlobalAlloc to allocate a block of memory to
> hold the graphic content, if I use the above method:
> Picture1 = LoadPictureA("mypicture.jpg") to put a picture to a picture box,
> is it necessary to free up the memory by GlobalFree function or it can free
> up by the PictureBox control?

The CreateStreamOnHGlobal() function can optionally take control of the global memory object (which is what's happening in this
case) and as such when the stream is released with the "pstm->Release();" line the hGlobal goes with it.

> Also, if the function has been called servel
> times to load different pictures, will it casue memory leak? How to avoid
> this?

If you run Task manager (Ctrl + Shift + Esc) and hit the processes tab you can see how much memory your process is using, running
this function a number of times should show that there is no resource leak there if you're worried about it. To be honest though if
that's what you're using then I'd use LoadPicture() instead and negate the need to carry around another DLL - they do pretty much
exactly the same thing and the VB runtime is written in C if you're worried about performance.
Hope this helps,

Mike


- Microsoft Visual Basic MVP -
E-Mail: EDais@xxxxxxxx
WWW: Http://EDais.mvps.org/


.



Relevant Pages

  • Re: Text overlay on image then Save to JPEG with reasonable DPI for Online printshop
    ... > Let's suppose I have an image of 1500 X 1500 pixels of which I ... picture at the top right of the card, or whether it is actually the card ... memory (an invisible VB picture box or a memory bitmap or DIBSection or ... What you "display to the user" while he is ...
    (comp.lang.basic.visual.misc)
  • Re: Image Processing with Geode GX2: Win CE or XPe?
    ... Few facts if you use color picture: If you use 2 bytes per pixel then you will have following memory bandwidth ... 1280x960x2x60> 140.65 MB/s for bringing RAW video over PCI-? ... Just for picture capture to memory you lost 140+ MB of bandwidth. ...
    (microsoft.public.windowsxp.embedded)
  • Re: i have to say this
    ... i was in my single room listening to dirty world ('89 sophomore year, ... from the guy then across the hall, I PROBABLY THOUGHT OF THE PICTURE ... moment, well, standing in a different place, which is why i thought it ... memory, because it was in the jeans and white t-shirt from 30th. ...
    (rec.music.dylan)
  • Re: do sodiimms for a dell laptop clock down like regular ram does?
    ... A neighbor has a Dell Latitude C600 laptop with PC100 memory. ... In regular motherboards I know pc133 will somehow clock itself down to pc100 but i'm hesitant to tell here to go ahead because its a Dell laptop. ... The difference is, the 8 chip module would use 32Mx8 chips, while ... The picture here looks more realistic for a low density module. ...
    (alt.comp.hardware.pc-homebuilt)
  • Re: do sodiimms for a dell laptop clock down like regular ram does?
    ... A neighbor has a Dell Latitude C600 laptop with PC100 memory. ... The difference is, the 8 chip module would use 32Mx8 chips, while ... The picture here looks more realistic for a low density module. ...
    (alt.comp.hardware.pc-homebuilt)