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. The Destructor can't cleanup the memory?

The Destructor can't cleanup the memory?

Scheduled Pinned Locked Moved C / C++ / MFC
csharpperformancehelpquestion
10 Posts 5 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.
  • S Offline
    S Offline
    signbit
    wrote on last edited by
    #1

    Hello, I have an unmanaged document class in my code, that looks like this

    class MyDocument
    {
    public:
    MyDocument()
    {
    // Initilize the dynamic structures throught call to 'new' operator
    }
    ~MyDocument()
    {
    // Cleanup the code
    this->CleanUp(); // When the function is called, it can't access the memory
    }
    bool CleanUp()
    {
    // Clean all the memory using 'delete' operator
    }
    };

    Then I have a pointer to it in my Form (that is a .NET managed class, a Form object).

    MyDocument *document;

    Inside the Constructor of the form, I initialize the document with

    this->document = new MyDocument();

    The Dispose() function of the Form has the code:

    if(disposing && document)
    delete document;

    During the lifetime of the program, several time i need to free the resouces of the document class and allocated new ones, like this:

    document->CleanUp(); // Code runs alright
    // allocated new resources to the document

    However when I close the application (i.e. the destructor of the form runs), I get an error that CleanUp() can't access the memory (because it is not accessible, may be it has been cleaned already). Could any one please explain why I can call CleanUp() anywhere in my program and I get no error (the memory gets cleaned as expected), and I get a 'Memory Not Accessible' error when I call it from the destrcutor. (I tried to comment out the code in the destructor, and the application gets cured i.e. It produces no errors/exceptions etc. - A programmer's national anthem; "AAAAAHHHHH!!!!"

    N 1 Reply Last reply
    0
    • S signbit

      Hello, I have an unmanaged document class in my code, that looks like this

      class MyDocument
      {
      public:
      MyDocument()
      {
      // Initilize the dynamic structures throught call to 'new' operator
      }
      ~MyDocument()
      {
      // Cleanup the code
      this->CleanUp(); // When the function is called, it can't access the memory
      }
      bool CleanUp()
      {
      // Clean all the memory using 'delete' operator
      }
      };

      Then I have a pointer to it in my Form (that is a .NET managed class, a Form object).

      MyDocument *document;

      Inside the Constructor of the form, I initialize the document with

      this->document = new MyDocument();

      The Dispose() function of the Form has the code:

      if(disposing && document)
      delete document;

      During the lifetime of the program, several time i need to free the resouces of the document class and allocated new ones, like this:

      document->CleanUp(); // Code runs alright
      // allocated new resources to the document

      However when I close the application (i.e. the destructor of the form runs), I get an error that CleanUp() can't access the memory (because it is not accessible, may be it has been cleaned already). Could any one please explain why I can call CleanUp() anywhere in my program and I get no error (the memory gets cleaned as expected), and I get a 'Memory Not Accessible' error when I call it from the destrcutor. (I tried to comment out the code in the destructor, and the application gets cured i.e. It produces no errors/exceptions etc. - A programmer's national anthem; "AAAAAHHHHH!!!!"

      N Offline
      N Offline
      Nibu babu thomas
      wrote on last edited by
      #2

      signbit wrote:

      bool CleanUp() { // Clean all the memory using 'delete' operator }

      Make sure you set all pointers to NULL after deleting them. For eg:

      delete pointer;
      pointer = NULL;


      Nibu thomas Software Developer

      S 1 Reply Last reply
      0
      • N Nibu babu thomas

        signbit wrote:

        bool CleanUp() { // Clean all the memory using 'delete' operator }

        Make sure you set all pointers to NULL after deleting them. For eg:

        delete pointer;
        pointer = NULL;


        Nibu thomas Software Developer

        S Offline
        S Offline
        signbit
        wrote on last edited by
        #3

        Yeah, I always set the pointer to NULL after it's been deleted to avoid confusion, my code looks like:

        if(NULL != pointer)
        {
        delete pointer;
        pointer = NULL;
        }

        - A programmer's national anthem; "AAAAAHHHHH!!!!"

        N N J S 4 Replies Last reply
        0
        • S signbit

          Yeah, I always set the pointer to NULL after it's been deleted to avoid confusion, my code looks like:

          if(NULL != pointer)
          {
          delete pointer;
          pointer = NULL;
          }

          - A programmer's national anthem; "AAAAAHHHHH!!!!"

          N Offline
          N Offline
          Nibu babu thomas
          wrote on last edited by
          #4

          signbit wrote:

          Yeah, I always set the pointer to NULL after it's been deleted to avoid confusion, my code looks like:

          Try to debug and find out. You should find something interesting. Set a breakpoint inside CleanUp, and step through.


          Nibu thomas Software Developer

          S 1 Reply Last reply
          0
          • N Nibu babu thomas

            signbit wrote:

            Yeah, I always set the pointer to NULL after it's been deleted to avoid confusion, my code looks like:

            Try to debug and find out. You should find something interesting. Set a breakpoint inside CleanUp, and step through.


            Nibu thomas Software Developer

            S Offline
            S Offline
            signbit
            wrote on last edited by
            #5

            Pointed Noted :) I'm trying it out... - A programmer's national anthem; "AAAAAHHHHH!!!!"

            1 Reply Last reply
            0
            • S signbit

              Yeah, I always set the pointer to NULL after it's been deleted to avoid confusion, my code looks like:

              if(NULL != pointer)
              {
              delete pointer;
              pointer = NULL;
              }

              - A programmer's national anthem; "AAAAAHHHHH!!!!"

              N Offline
              N Offline
              Nick_Kisialiou
              wrote on last edited by
              #6

              Why do you need such a code? As far as I remember "delete NULL;" is a valid instruction, that is delete knows about NULL and know how to handle it. Basically, there is something similar to your "if(NULL != pointer)" within implementation of delete, so there is no need for extra one.

              1 Reply Last reply
              0
              • S signbit

                Yeah, I always set the pointer to NULL after it's been deleted to avoid confusion, my code looks like:

                if(NULL != pointer)
                {
                delete pointer;
                pointer = NULL;
                }

                - A programmer's national anthem; "AAAAAHHHHH!!!!"

                J Offline
                J Offline
                Johann Gerell
                wrote on last edited by
                #7

                No need to check for NULL before deleting a pointer - it's a benign operation, as free(...) in C. -- The Blog: Bits and Pieces

                1 Reply Last reply
                0
                • S signbit

                  Yeah, I always set the pointer to NULL after it's been deleted to avoid confusion, my code looks like:

                  if(NULL != pointer)
                  {
                  delete pointer;
                  pointer = NULL;
                  }

                  - A programmer's national anthem; "AAAAAHHHHH!!!!"

                  S Offline
                  S Offline
                  Stephen Hewitt
                  wrote on last edited by
                  #8

                  In C++ delete NULL is a NOP - It does nothing. You don't need the if statement in the code above, the following is equivalent:

                  delete pointer;
                  pointer = NULL;
                  

                  If you put the if you're checking twice and the code is longer then need be (more chance of making a mistake). Steve

                  S 1 Reply Last reply
                  0
                  • S Stephen Hewitt

                    In C++ delete NULL is a NOP - It does nothing. You don't need the if statement in the code above, the following is equivalent:

                    delete pointer;
                    pointer = NULL;
                    

                    If you put the if you're checking twice and the code is longer then need be (more chance of making a mistake). Steve

                    S Offline
                    S Offline
                    signbit
                    wrote on last edited by
                    #9

                    Okay, Okay, but why the destructor can't cleanup? - A programmer's national anthem; "AAAAAHHHHH!!!!"

                    S 1 Reply Last reply
                    0
                    • S signbit

                      Okay, Okay, but why the destructor can't cleanup? - A programmer's national anthem; "AAAAAHHHHH!!!!"

                      S Offline
                      S Offline
                      Stephen Hewitt
                      wrote on last edited by
                      #10

                      I can't see anything wrong - Can you provide more detail? Steve

                      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