|
-
Jul 8th, 2001, 09:55 AM
#1
Thread Starter
PowerPoster
Releasing DCs! Kedaman, anyone?
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:
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..
Last edited by Fox; Jul 8th, 2001 at 10:00 AM.
-
Jul 8th, 2001, 10:48 AM
#2
Lively Member
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
Visit EliteVB for slimy subclassing tricks and GDI32 goodness.
-
Jul 8th, 2001, 01:44 PM
#3
Thread Starter
PowerPoster
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)
Code:
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?
Last edited by Fox; Jul 8th, 2001 at 01:47 PM.
-
Jul 8th, 2001, 01:47 PM
#4
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*
-
Jul 8th, 2001, 01:48 PM
#5
Thread Starter
PowerPoster
Sure I use graphics in VB Never seen my website? *hehe*
-
Jul 8th, 2001, 01:49 PM
#6
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..
-
Jul 8th, 2001, 02:13 PM
#7
Frenzied Member
Try deleting the DC first, and then release it...
-
Jul 8th, 2001, 02:18 PM
#8
Thread Starter
PowerPoster
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..
-
Jul 8th, 2001, 02:23 PM
#9
Well, I'm still disapointed in you
-
Jul 8th, 2001, 02:47 PM
#10
Thread Starter
PowerPoster

*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???" );
-
Jul 8th, 2001, 02:58 PM
#11
*kick* *kick*
I know your games are better... I'm still learning Dx and OGL...
The solution to your problem is to use C++ 
Where in C++ is the Print command, BTW?
-
Jul 8th, 2001, 03:03 PM
#12
Thread Starter
PowerPoster
Aaaahahhh!!
*runs away shouting*
-
Jul 8th, 2001, 05:42 PM
#13
-
Jul 8th, 2001, 08:59 PM
#14
Fanatic Member
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....
Code:
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
{Insert random techno-babble here}
{Insert quote from some long gone mofo here}
-
Jul 9th, 2001, 12:17 AM
#15
Thread Starter
PowerPoster
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...?!
-
Jul 9th, 2001, 01:45 AM
#16
transcendental analytic
why oh why...
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'<-- 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
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jul 9th, 2001, 01:55 AM
#17
Frenzied Member
Government is another way to say better…than…you.
It’s like ice but no pick, a murder charge that won’t stick,
it’s like a whole other world where you can smell the food,
but you can’t touch the silverware.
Huh, what luck. Fascism you can vote for.
Humph, isn’t that sweet?
And we’re all gonna die some day, because that’s the American way
-Stone Sour
-
Jul 9th, 2001, 02:47 AM
#18
Thread Starter
PowerPoster
Why doesn't it work anyways kedaman?
I tried this code:
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..
-
Jul 9th, 2001, 12:34 PM
#19
Fanatic Member
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...
Code:
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.
{Insert random techno-babble here}
{Insert quote from some long gone mofo here}
-
Jul 9th, 2001, 01:52 PM
#20
Thread Starter
PowerPoster
Ok, this seems to work now. Thanks!
-
Jul 9th, 2001, 02:06 PM
#21
Frenzied Member
Hey Fox, don't forget to update the BitBlt tutorial in your site
-
Jul 9th, 2001, 02:34 PM
#22
transcendental analytic
You crazy blitters
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jul 9th, 2001, 03:21 PM
#23
Thread Starter
PowerPoster
Ya I will..
Blit, blit, hurray!
-
Jul 9th, 2001, 03:57 PM
#24
Fanatic Member
And....
Don't forget to show your gratitude towards your fellow VB-World brethren either.
{Insert random techno-babble here}
{Insert quote from some long gone mofo here}
-
Jul 9th, 2001, 03:58 PM
#25
lpDDSPrimary->BltFast, lpDDSPrimary->BltFast Hurray!
-
Jul 9th, 2001, 06:24 PM
#26
Frenzied Member
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!
-
Jul 10th, 2001, 11:00 AM
#27
transcendental analytic
You really crazy blitters
bohooo! Bltfast won't work with clippers
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jul 11th, 2001, 10:23 AM
#28
Addicted Member
bah, my RPG will own all of yourz! KYAHAHAHA....
**sees everyone grab a torch / pitchfork**
....................AHHHH
To understand recursion, one must first understand the concept of recursion.
-
Jul 11th, 2001, 04:19 PM
#29
-
Jul 11th, 2001, 07:03 PM
#30
Addicted Member
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)
To understand recursion, one must first understand the concept of recursion.
-
Jul 11th, 2001, 09:39 PM
#31
Lively Member
'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
Visit EliteVB for slimy subclassing tricks and GDI32 goodness.
-
Jul 11th, 2001, 10:17 PM
#32
Addicted Member
I'm telling you, there's not a programmer in the world that can draw. It's the law of life :((((
To understand recursion, one must first understand the concept of recursion.
-
Jul 11th, 2001, 10:19 PM
#33
Good Ol' Platypus
I'm okay at it... a lot of people say I'm really good, but I wouldn't know by your standards...
All contents of the above post that aren't somebody elses are mine, not the property of some media corporation. 
(Just a heads-up)
-
Jul 12th, 2001, 04:12 AM
#34
Addicted Member
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
Some Days, i just get this feeling that i'm helping to write dozens of Viruses...
-
Jul 12th, 2001, 02:41 PM
#35
transcendental analytic
You should worry about your programming skills more than anything. They will fail you
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jul 13th, 2001, 01:48 AM
#36
Thread Starter
PowerPoster
*hehe* not sure.. I cant work on a game too long without nice graphics to create test levels
-
Jul 13th, 2001, 07:44 AM
#37
Frenzied Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|