Re: Free up allocated memory
- From: "Mike D Sutton" <EDais@xxxxxxxx>
- Date: Sun, 1 May 2005 19:35:43 +0100
> 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/
.
- References:
- Free up allocated memory
- From: akuma
- Free up allocated memory
- Prev by Date: Free up allocated memory
- Next by Date: A 'skinned' application
- Previous by thread: Free up allocated memory
- Next by thread: A 'skinned' application
- Index(es):
Relevant Pages
|