Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to delete an object from DC

How to delete an object from DC

Scheduled Pinned Locked Moved Visual Basic
helpc++visual-studiographicsjson
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mirano
    wrote on last edited by
    #1

    Hi guys. This one is really driving me crazy. I create a memory DC, and load a bitmap in it: MemDC = CreateCompatibleDC(0) hBitmap = LoadImageA(App.hInstance, szPath, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE) SelectObject MemDC, hBitmap and it works fine, I can use it in processing with BitBlt function, now here comes the problem: lRes = DeleteDC(MemDC) If lRes = 0 Then 'We have an error deleting a DC, do something... End If lRes = DeleteObject(hBitmap) If lRes = 0 then 'We have an error deleting an object, do something... End If but even tho it shows that it has deleted the hBitmap object, it actually didn't, cause I can get its handle from Immediate Pane by typing ?hBitmap. It is impossible to delete hBitmap from memory, and I effectively have a memory leak. Every time I create that object, it never gets deleted from the memory. Trying to delete it before I delete DC doesn't work either, cause DC flags it as being used and thus not deletable. In C++ it is easy, you create an pOldBitmap at the beginning of the function and then you select it back into DC at the end of the function, so you can delete the bitmap you created, or you use CDC::SaveDC which is fine for MFC but does not have a related function in API. But how to do it here in VB with API functions? I spent some serious time trying to figure this out, but couldn't. When I check a MemDC in the ImmediatePane of my IDE, like ?MemDC it shows NULL, so it really gets deleted, but if I do ?hBitmap then it shows something like 16110, and even 10 minutes after it will show the same, so the memory never got reclaimed. Any help would be appreciated. Thanks. .

    A 1 Reply Last reply
    0
    • M mirano

      Hi guys. This one is really driving me crazy. I create a memory DC, and load a bitmap in it: MemDC = CreateCompatibleDC(0) hBitmap = LoadImageA(App.hInstance, szPath, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE) SelectObject MemDC, hBitmap and it works fine, I can use it in processing with BitBlt function, now here comes the problem: lRes = DeleteDC(MemDC) If lRes = 0 Then 'We have an error deleting a DC, do something... End If lRes = DeleteObject(hBitmap) If lRes = 0 then 'We have an error deleting an object, do something... End If but even tho it shows that it has deleted the hBitmap object, it actually didn't, cause I can get its handle from Immediate Pane by typing ?hBitmap. It is impossible to delete hBitmap from memory, and I effectively have a memory leak. Every time I create that object, it never gets deleted from the memory. Trying to delete it before I delete DC doesn't work either, cause DC flags it as being used and thus not deletable. In C++ it is easy, you create an pOldBitmap at the beginning of the function and then you select it back into DC at the end of the function, so you can delete the bitmap you created, or you use CDC::SaveDC which is fine for MFC but does not have a related function in API. But how to do it here in VB with API functions? I spent some serious time trying to figure this out, but couldn't. When I check a MemDC in the ImmediatePane of my IDE, like ?MemDC it shows NULL, so it really gets deleted, but if I do ?hBitmap then it shows something like 16110, and even 10 minutes after it will show the same, so the memory never got reclaimed. Any help would be appreciated. Thanks. .

      A Offline
      A Offline
      Anonymous
      wrote on last edited by
      #2

      You code this written good, safe with some small corrections. MemDC = CreateCompatibleDC(hdc) hBitmap = LoadImageA(App.hInstance, szPath, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE) hOldBitmap = SelectObject( MemDC, hBitmap ) ' bla bla bla SelectObject MemDC, hOldBitmap lRes = DeleteDC(MemDC) If lRes = 0 Then 'We have an error deleting a DC, do something... End If lRes = DeleteObject(hBitmap) If lRes = 0 then 'We have an error deleting an object, do something... End If Now the explication: When call to the function DeleteDCo or DeleteObject, this delete the object from the memory, but not set to zero the variable param. It is why you sees in the immediate window the same value of handle when typing "?hBitmap ". The unquestionable test that bitmap was destroyed is to try to use his handle after to have called to DeleteObject(hBitmap). Good Luck!! MrSparc.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups