Correct me if I'm wrong, but surely the code below should be able to run indefinitely, as it is deleting the DC right after it creates it ??

If not, then how does one actually recover the lost resources ?

VB Code:
  1. Option Explicit
  2.  
  3. Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
  4. Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hDc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
  5. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDc As Long) As Long
  6. Private Declare Function DeleteDC Lib "gdi32" (ByVal hDc As Long) As Long
  7. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  8. Private Declare Function SelectObject Lib "gdi32" (ByVal hDc As Long, ByVal hObject As Long) As Long
  9. Private Declare Sub GlobalMemoryStatus Lib "kernel32" (lpBuffer As MEMORYSTATUS)
  10.  
  11. Private Type MEMORYSTATUS
  12.     dwLength As Long
  13.     dwMemoryLoad As Long
  14.     dwTotalPhys As Long
  15.     dwAvailPhys As Long
  16.     dwTotalPageFile As Long
  17.     dwAvailPageFile As Long
  18.     dwTotalVirtual As Long
  19.     dwAvailVirtual As Long
  20. End Type
  21.  
  22.  
  23. Private Function GenerateDC(FileName As String) As Long
  24.     Dim DC As Long, picTemp As IPictureDisp
  25.     DC = CreateCompatibleDC(0)
  26.     If DC < 1 Then
  27.         Exit Function
  28.     End If
  29.     Set picTemp = LoadPicture(FileName)
  30.     SelectObject DC, picTemp
  31.     DeleteObject picTemp
  32.     Set picTemp = Nothing
  33.     GenerateDC = DC
  34. End Function
  35.  
  36. Private Sub Form_Load()
  37.     Dim i As Long
  38.     Do
  39.         DoEvents
  40.         Debug.Print "Memory Free : " & (x.dwAvailPhys / 1024) & "Kb  :  " & IIf(DeleteDC(GenerateDC("c:\boilogo.gif")) <> 0, "DeleteDC() Worked", "DeleteDC() Failed")
  41.         DoEvents
  42.     Loop
  43. End Sub