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. C / C++ / MFC
  4. how do i use DeleteObject()...

how do i use DeleteObject()...

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++performancehelpannouncement
3 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.
  • J Offline
    J Offline
    John Cruz
    wrote on last edited by
    #1

    do i use delete object like this to prevent resource leaks??? // Destructor CBoard::~CBoard() { // Delete the object to prevent resource leaks mpPen->DeleteObject(); mpFont->DeleteObject(); // Destroys all the pointers to all objects delete mpPen; delete mpFont; for (int r = 0; r < MAXROW; r++) for (int c= 0; c < MAXCOL; c++) delete mpGrid[r][c]; } or i can just destroy it by deleting it or do i have to release it before i delete it when i called it like this: // Change the Font Height and Width to the values entered void CBoard::SetFontSize(CDC* pDC, int Height, int Width) { mpFont = new CFont; VERIFY(mpFont->CreateFont(Height,Width,0,0,0,0,0,0,0,0,0,0, DEFAULT_PITCH|FF_DONTCARE,"Tictactoe")); VERIFY(mpDefaultFont = pDC->SelectObject(mpFont)); } anyways, can anyone tell me if i am doing it right or what i am doing can cause memory or resource leaks... well, thank you in advance for your help :) :) John

    C 1 Reply Last reply
    0
    • J John Cruz

      do i use delete object like this to prevent resource leaks??? // Destructor CBoard::~CBoard() { // Delete the object to prevent resource leaks mpPen->DeleteObject(); mpFont->DeleteObject(); // Destroys all the pointers to all objects delete mpPen; delete mpFont; for (int r = 0; r < MAXROW; r++) for (int c= 0; c < MAXCOL; c++) delete mpGrid[r][c]; } or i can just destroy it by deleting it or do i have to release it before i delete it when i called it like this: // Change the Font Height and Width to the values entered void CBoard::SetFontSize(CDC* pDC, int Height, int Width) { mpFont = new CFont; VERIFY(mpFont->CreateFont(Height,Width,0,0,0,0,0,0,0,0,0,0, DEFAULT_PITCH|FF_DONTCARE,"Tictactoe")); VERIFY(mpDefaultFont = pDC->SelectObject(mpFont)); } anyways, can anyone tell me if i am doing it right or what i am doing can cause memory or resource leaks... well, thank you in advance for your help :) :) John

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      The most important step is selecting the original pen/brush/font into the DC before destroying it. OTherwise you will leak GDI resources and your program will eventually kill the graphics subsystem on any non-NT based OS. You don't need to call DeleteObject before calling delete. Delete allows you to delete the handle being encapsulated without destroying the class object. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little.

      J 1 Reply Last reply
      0
      • C Christian Graus

        The most important step is selecting the original pen/brush/font into the DC before destroying it. OTherwise you will leak GDI resources and your program will eventually kill the graphics subsystem on any non-NT based OS. You don't need to call DeleteObject before calling delete. Delete allows you to delete the handle being encapsulated without destroying the class object. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little.

        J Offline
        J Offline
        John Cruz
        wrote on last edited by
        #3

        Thanks for the help Christian. ill remember that :)

        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