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. ailFreeing memory

ailFreeing memory

Scheduled Pinned Locked Moved C / C++ / MFC
performancetutorialannouncementlounge
3 Posts 3 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.
  • L Offline
    L Offline
    laiju
    wrote on last edited by
    #1

    This may be a general doubt but I do need clarification . I have declared a userdefined class object m_pElement as a member of CMainFrame. I want to release its memory in the destructor in the foll.manner. CMainFrame::~CMainFrame() { if (m_pElement) delete m_Element; } But if I had not declared memory for pElement using 'new' the above destruction leads to exception. How to check for allocated memory and then release the memory for pElement. laiju

    C B 2 Replies Last reply
    0
    • L laiju

      This may be a general doubt but I do need clarification . I have declared a userdefined class object m_pElement as a member of CMainFrame. I want to release its memory in the destructor in the foll.manner. CMainFrame::~CMainFrame() { if (m_pElement) delete m_Element; } But if I had not declared memory for pElement using 'new' the above destruction leads to exception. How to check for allocated memory and then release the memory for pElement. laiju

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      In the class constructor of CMainFrame, set pointer to NULL: CMainFrame::CMainFrame() { m_pElement = NULL; } Like that, in your destructor, if the pointer is NULL, it won't be destroyed.

      1 Reply Last reply
      0
      • L laiju

        This may be a general doubt but I do need clarification . I have declared a userdefined class object m_pElement as a member of CMainFrame. I want to release its memory in the destructor in the foll.manner. CMainFrame::~CMainFrame() { if (m_pElement) delete m_Element; } But if I had not declared memory for pElement using 'new' the above destruction leads to exception. How to check for allocated memory and then release the memory for pElement. laiju

        B Offline
        B Offline
        Budric B
        wrote on last edited by
        #3

        Set m_pElement to NULL in your constructor. Then, the if statement will actually work and the delete will not be executed. And even if it is, it's not a problem to delete a pointer to NULL. In your case however, m_pElement can point to 0x0ccccc (arbitrary) and you're trying to delete it which is not legal.

        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