Results 1 to 37 of 37

Thread: Releasing DCs! Kedaman, anyone?

  1. #1

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088

    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.

  2. #2
    Lively Member The Hand's Avatar
    Join Date
    Jun 2001
    Posts
    88
    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.

  3. #3

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    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.

  4. #4
    denniswrenn
    Guest
    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*

  5. #5

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Sure I use graphics in VB Never seen my website? *hehe*

  6. #6
    denniswrenn
    Guest
    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..

  7. #7
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Try deleting the DC first, and then release it...
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  8. #8

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    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..

  9. #9
    denniswrenn
    Guest
    Well, I'm still disapointed in you

  10. #10

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088


    *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???" );

  11. #11
    denniswrenn
    Guest
    *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?

  12. #12

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Aaaahahhh!!

    *runs away shouting*

  13. #13
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Well Fox, I guess that "Sam's teach yourself C++ in 24 hours" is not that good after all

    Hum, btw, are you The Fox that made all those cool Graal levels?
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  14. #14
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    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}

  15. #15

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    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...?!

  16. #16
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    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.

  17. #17
    Frenzied Member Skitchen8's Avatar
    Join Date
    Feb 2001
    Location
    Binghamotn, NY
    Posts
    1,943
    Originally posted by Jotaf98
    Well Fox, I guess that "Sam's teach yourself C++ in 24 hours" is not that good after all

    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
    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

  18. #18

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    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..

  19. #19
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    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}

  20. #20

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Ok, this seems to work now. Thanks!


  21. #21
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Hey Fox, don't forget to update the BitBlt tutorial in your site
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  22. #22
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  23. #23

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Ya I will..

    Blit, blit, hurray!

  24. #24
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987

    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}

  25. #25
    denniswrenn
    Guest
    lpDDSPrimary->BltFast, lpDDSPrimary->BltFast Hurray!

  26. #26
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    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!
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  27. #27
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  28. #28
    Addicted Member DarkMoose's Avatar
    Join Date
    Jul 2000
    Location
    in a box
    Posts
    185
    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.

  29. #29
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    (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?
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  30. #30
    Addicted Member DarkMoose's Avatar
    Join Date
    Jul 2000
    Location
    in a box
    Posts
    185
    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.

  31. #31
    Lively Member The Hand's Avatar
    Join Date
    Jun 2001
    Posts
    88
    '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.

  32. #32
    Addicted Member DarkMoose's Avatar
    Join Date
    Jul 2000
    Location
    in a box
    Posts
    185
    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.

  33. #33
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    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)

  34. #34
    Addicted Member
    Join Date
    Apr 2000
    Location
    England
    Posts
    246
    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...

  35. #35
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  36. #36

    Thread Starter
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    *hehe* not sure.. I cant work on a game too long without nice graphics to create test levels

  37. #37
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    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
    Attached Images Attached Images  
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width