Click to See Complete Forum and Search --> : Releasing DCs! Kedaman, anyone?
Fox
Jul 8th, 2001, 09:55 AM
What is the correct way to release a memory DC? I tried ReleaseDC and DeleteDC but both seem to leave a memory lack...
Here's my code:
Function LoadDC(iFileName As String) As Long
Dim Temp As IPictureDisp
Dim DC As Long
'Create compatible DC
DC = CreateCompatibleDC(MainWindow.hdc)
'Load bitmap
Set Temp = LoadPicture(iFileName)
SelectObject DC, Temp
'Apply values
LoadDC = DC
'Release memory
DeleteObject Temp
Set Temp = Nothing
End Function
Sub UnloadDC(iDC As Long)
'Release DCs
ReleaseDC MainWindow.hWnd, iDC
DeleteDC iDC
End Sub
The mistake maybe elsewhere, but I assume it's somewhere around this functions...
I also noticed Windows does free the resources lost as soon as I close VB..
The Hand
Jul 8th, 2001, 10:48 AM
Order is a very important thing when using the GDI32 API.
You don't need to call the "ReleaseDC" in the example you showed. You should just call "DeleteDC".
ReleaseDC is generally used when you grab a window's or control's DC using the "GetDC" function (hence the inclusion of the hWnd argument). When you call ReleaseDC, my understanding is that you are giving ownership of the device context back to its control.
Since you are creating a new one, you should just delete it. By releasing the DC first, you may make it impossible to delete the DC in the subsequent statement.
I could be wrong, but thats my 2 cents.
-Hand out
Fox
Jul 8th, 2001, 01:44 PM
Nope. I just have both functions there because I hoped one will work..
I just made a sample project that loads 100 DCs from a file and both methods to release them (I runned a compiled exe). Heres a protocol of my tests: (text and system/user/gfx resources)
1) Started the program (92/92/95)
2) Loaded 100 DCs (86,92,86)
3) ReleasDC for all (86,92,86)
4) Exit program (92/92/95)
5) Started again (92/92/94)
6) Loaded 100 DCs again (86/92/86)
7) DeleteDC for all (86,92,86)
8) Exit program (92,92,95)
9) Started last time (92,92,94)
10) And exit again (92,92,95)
As you see the resources aren't really lost, but as long as the program runs I have a problem! So what is the correct way to release the DCs?
denniswrenn
Jul 8th, 2001, 01:47 PM
You're using graphics in VB? I'm ashamed of you :( I was going to reply to use the CS_OWNDC as the style in your window class, so you didn't have to create one, but I guess I'll just take it elsewhere..... *hummmf*
Fox
Jul 8th, 2001, 01:48 PM
Sure I use graphics in VB ;) Never seen my website? *hehe*
denniswrenn
Jul 8th, 2001, 01:49 PM
Yeah, but after rumaging through my back up CDs, I found the LeMitsch game you sent me a while back(And the source).... and it was in C++, so I just assumed that's what you'd moved to for graphics..
Jotaf98
Jul 8th, 2001, 02:13 PM
Try deleting the DC first, and then release it...
Fox
Jul 8th, 2001, 02:18 PM
a) I don't have this problem in C/DirectX
b) I didn't say I make a game in VB, just graphics
c) @Jotaf: Nothing happens..
denniswrenn
Jul 8th, 2001, 02:23 PM
Well, I'm still disapointed in you ;)
Fox
Jul 8th, 2001, 02:47 PM
:(
*boohoooo*
*sighs*
if (you_make_games_in_cpp) Print( "I bet my VB games are still better than your C++ games *hihi* :)" ); else Print( "Well, do you know a solution for the problem???" );
denniswrenn
Jul 8th, 2001, 02:58 PM
*kick* *kick*
I know your games are better... I'm still learning Dx and OGL...
The solution to your problem is to use C++ :p
Where in C++ is the Print command, BTW? :rolleyes:
Fox
Jul 8th, 2001, 03:03 PM
Aaaahahhh!!
*runs away shouting*
Jotaf98
Jul 8th, 2001, 05:42 PM
Well Fox, I guess that "Sam's teach yourself C++ in 24 hours" is not that good after all :p
Hum, btw, are you The Fox that made all those cool Graal levels? ;)
YoungBuck
Jul 8th, 2001, 08:59 PM
You need to select all the objects that were created with the DC back into it before using DeleteDC and then Delete all the objects that you created (the Temp variable in your example), SelectObject's return value is the handle to the object currently being replaced, here is something I put together to test this....
Option Explicit
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Private Declare Sub GlobalMemoryStatus Lib "kernel32" (lpBuffer As MEMORYSTATUS)
Private Type MEMORYSTATUS
dwLength As Long
dwMemoryLoad As Long
dwTotalPhys As Long
dwAvailPhys As Long
dwTotalPageFile As Long
dwAvailPageFile As Long
dwTotalVirtual As Long
dwAvailVirtual As Long
End Type
Private Sub Form_Load()
Dim hdc As Long
Dim hPen As Long
Dim hOrigPen As Long
Dim hwndDesktop
Dim hDesktop
Dim iCt As Long
MsgBox "PHYS MEM BEFORE DC OPERATIONS: " & GetAvailableMemory
For iCt = 0 To 10000
' get the desktop's window handle
hwndDesktop = GetDesktopWindow()
' get the handle to the desktop's DC
hDesktop = GetDC(hwndDesktop)
' create a DC compatible with the Desktop
hdc = CreateCompatibleDC(hDesktop)
' Create a pen
hPen = CreatePen(0, 1, &H0)
' select the pen into the new DC and retrieve the handle _
to the pen that was created with the DC
hOrigPen = SelectObject(hdc, hPen)
'before you use DeleteDC you need to put all objects back into _
DC and remove the ones created yourself
SelectObject hdc, hOrigPen
' delete the pen that we created
DeleteObject hPen
' release the DC retrieved from the desktop
ReleaseDC hwndDesktop, hDesktop
' delete the DC including the objects created with the DC
DeleteDC hdc
Next iCt
MsgBox "PHYS MEM AFTER DC OPERATIONS: " & GetAvailableMemory
End Sub
Private Function GetAvailableMemory() As Long
Dim m As MEMORYSTATUS
GlobalMemoryStatus m
GetAvailableMemory = m.dwAvailPhys
End Function
Fox
Jul 9th, 2001, 12:17 AM
Hm, thanks.. but why do you need a pen here?
Anyways, if you look at my first code it does exactly the same as yours, first SelectObject, then DeleteObject, ReleaseDC and finally DeleteDC...?!
kedaman
Jul 9th, 2001, 01:45 AM
Function LoadDC(iFileName As String) As Long
Dim Temp As IPictureDisp
Dim DC As Long
'Create compatible DC
DC = CreateCompatibleDC(MainWindow.hdc)
'Load bitmap
Set Temp = LoadPicture(iFileName)
SelectObject DC, Temp
'Apply values
LoadDC = DC
'Release memory
DeleteObject Temp'<-- This will delete the selected bitmap fox! remove this line.
Set Temp = Nothing'<-- this is meaningless, temp will run out of scope at the next line
End Function
Sub UnloadDC(iDC As Long)
'Release DCs
ReleaseDC MainWindow.hWnd, iDC'<-- no way!
DeleteDC iDC'<-- the selected bitmap was not released, here's what to do:
DeleteObject SelectObject(iDC,0)
DeleteDC iDC
End Sub
*Ahem* I thought you of all gameprogammers should know this ;)
Skitchen8
Jul 9th, 2001, 01:55 AM
Originally posted by Jotaf98
Well Fox, I guess that "Sam's teach yourself C++ in 24 hours" is not that good after all :p
Hum, btw, are you The Fox that made all those cool Graal levels? ;)
oh cool... im not the only one that bought that worthless piece of sh*t of a book :D
Fox
Jul 9th, 2001, 02:47 AM
Why doesn't it work anyways kedaman?
I tried this code:
dim a as long
dim dc as long
dim temp as ipicturedisp
for a=0 to 100
'Load
dc = createcompatibledc(me.hdc)
set temp=loadpicture("c:\test.bmp")
selectobject dc, temp
set temp=nothing
'Release
deleteobject selectobject(dc, 0)
deletedc dc
next
And if I right-understood your corrections the function above shouldn't waste any memory..? Well it does..
YoungBuck
Jul 9th, 2001, 12:34 PM
The only reason I put the code to add the Pen was to just illustrate the fact that you have to put back all the objects originally created with the DC before you use DeleteDC, when you create a DC it comes stocked with a set of objects (a Pen, a Brush, a Bitmap, etc.) and you have to Select these objects back into the DC before you can use DeleteDC or you will have memory leaks as you are experiencing right now. So to fix your code you need to do this...
dim a as long
dim dc as long
dim temp as ipicturedisp
dim orig as Long
for a=0 to 100
'Load
dc = createcompatibledc(me.hdc)
set temp=loadpicture("c:\test.bmp")
' get handle to original bitmap created with DC
orig = selectobject(dc, temp)
'Release
' put the original bitmap back into the DC
selectobject dc, orig
' delete the bitmap that was created
deleteobject temp
' free the DC's resources
deletedc dc
next
Hope that is clearer.
Fox
Jul 9th, 2001, 01:52 PM
Ok, this seems to work now. Thanks!
:)
Jotaf98
Jul 9th, 2001, 02:06 PM
Hey Fox, don't forget to update the BitBlt tutorial in your site ;)
kedaman
Jul 9th, 2001, 02:34 PM
You crazy blitters ;)
Fox
Jul 9th, 2001, 03:21 PM
Ya I will..
Blit, blit, hurray!
YoungBuck
Jul 9th, 2001, 03:57 PM
Don't forget to show your gratitude towards your fellow VB-World brethren either. ;)
denniswrenn
Jul 9th, 2001, 03:58 PM
lpDDSPrimary->BltFast, lpDDSPrimary->BltFast Hurray! :rolleyes:
Jotaf98
Jul 9th, 2001, 06:24 PM
lpDDSPrimary->BltFast, lpDDSPrimary->BltFast Hurray!
lpDDSPrimary->BltFast, lpDDSPrimary->BltFast Hurray!
lpDDSPrimary->BltFast, lpDDSPrimary->BltFast Hurray!
lpDDSPrimary->BltFast, lpDDSPrimary->BltFast Hurray!
lpDDSPrimary->BltFast, lpDDSPrimary->BltFast Hurray!
lpDDSPrimary->BltFast, lpDDSPrimary->BltFast Hurray!
lpDDSPrimary->BltFast, lpDDSPrimary->BltFast Hurray!
lpDDSPrimary->BltFast, lpDDSPrimary->BltFast Hurray!
lpDDSPrimary->BltFast, lpDDSPrimary->BltFast Hurray!
lpDDSPrimary->BltFast, lpDDSPrimary->BltFast Hurray!
lpDDSPrimary->BltFast, lpDDSPrimary->BltFast Hurray!
lpDDSPrimary->BltFast, lpDDSPrimary->BltFast Hurray!
lpDDSPrimary->BltFast, lpDDSPrimary->BltFast Hurray!
lpDDSPrimary->BltFast, lpDDSPrimary->BltFast Hurray!
lpDDSPrimary->BltFast, lpDDSPrimary->BltFast Hurray!
lpDDSPrimary->BltFast, lpDDSPrimary->BltFast Hurray!
lpDDSPrimary->BltFast, lpDDSPrimary->BltFast Hurray!
lpDDSPrimary->BltFast, lpDDSPrimary->BltFast Hurray!
lpDDSPrimary->BltFast, lpDDSPrimary->BltFast Hurray!
lpDDSPrimary->BltFast, lpDDSPrimary->BltFast Hurray!
lpDDSPrimary->BltFast, lpDDSPrimary->BltFast Hurray!
lpDDSPrimary->BltFast, lpDDSPrimary->BltFast Hurray!
kedaman
Jul 10th, 2001, 11:00 AM
You really crazy blitters
bohooo! Bltfast won't work with clippers :(
DarkMoose
Jul 11th, 2001, 10:23 AM
bah, my RPG will own all of yourz! KYAHAHAHA....
**sees everyone grab a torch / pitchfork**
....................AHHHH
Jotaf98
Jul 11th, 2001, 04:19 PM
(Suddenly, everyone that has a torch sticks it to the dungeon's walls and everyone else makes a circle, and then undeads start to appear and...)
Ok, time for me to shut up :)
Anyway, if your RPG is sooo good, could you please send us a screenshot of it? ;)
DarkMoose
Jul 11th, 2001, 07:03 PM
Ugh, bad question. Sure the programming is good, but ..... I'm drawing the graphics myself X[ I just can't find tilesets anywhere so...
Well I'll be sure to tell all of you when my RPG is done (10 years from now)
The Hand
Jul 11th, 2001, 09:39 PM
'Tis the crux of the problem, isn't it?
I've got mad programming & GDI32 skills too, but no time and no artistic ability...
**boo hoo** :(
-Hand out
DarkMoose
Jul 11th, 2001, 10:17 PM
I'm telling you, there's not a programmer in the world that can draw. It's the law of life :((((
Sastraxi
Jul 11th, 2001, 10:19 PM
I'm okay at it... a lot of people say I'm really good, but I wouldn't know by your standards...
Nirces
Jul 12th, 2001, 04:12 AM
I nicked my tileset from the UO CD using a program called Inside UO.
1000's of tiles (non-rotated) ready to go, all 64x64
kedaman
Jul 12th, 2001, 02:41 PM
You should worry about your programming skills more than anything. They will fail you :p
Fox
Jul 13th, 2001, 01:48 AM
*hehe* not sure.. I cant work on a game too long without nice graphics to create test levels ;)
Jotaf98
Jul 13th, 2001, 07:44 AM
Well I'm not THAT bad at graphics, I just don't have the right tools, and hand-drawn graphics usually don't look very good in a game :)
Look at this small image I drew in my eraser during a boring History class ;)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.