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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to delete *ptr correctly?

How to delete *ptr correctly?

Scheduled Pinned Locked Moved C / C++ / MFC
c++debugginghelptutorialquestion
8 Posts 6 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.
  • U Offline
    U Offline
    uus99
    wrote on last edited by
    #1

    I made a very simple class, and when the default destructor is called, i get a debug assertion failure. (i'm using VC++ SP5) if i call a function like this void Testfunction() { uImage test; test.Create(100); } the function exits properly, with the default destructor deleting the arrays successfully, however,if i call a function like this, that is without allocating the arrays, void Testfunction() { uImage test; } The function exits with a Debug Assertion Failure, _CrtIsValidHeapPointer What i'm confused is that, i'm trying to do cleanup in the destructor by deleting all the dynamic arrays. Although i havent created the arrays, there should be no problem coz i checked whether the pointers are NULL or not (when performing delete). So, why is there a debug assertion? My class header is #ifndef UIMAGE_H #define UIMAGE_H class uImage { private: int *R; int *G; int *B; public: uImage(); ~uImage(); void Create(int x); }; #endif My Class is #include "StdAfx.h" #include "uImage.h" uImage::uImage() { } uImage::~uImage() { if(R!=NULL) delete R; if(G!=NULL) delete G; if(B!=NULL) delete B; } void uImage::Create(int x) { R=new int[x]; G=new int[x]; B=new int[x]; }

    D A 2 Replies Last reply
    0
    • U uus99

      I made a very simple class, and when the default destructor is called, i get a debug assertion failure. (i'm using VC++ SP5) if i call a function like this void Testfunction() { uImage test; test.Create(100); } the function exits properly, with the default destructor deleting the arrays successfully, however,if i call a function like this, that is without allocating the arrays, void Testfunction() { uImage test; } The function exits with a Debug Assertion Failure, _CrtIsValidHeapPointer What i'm confused is that, i'm trying to do cleanup in the destructor by deleting all the dynamic arrays. Although i havent created the arrays, there should be no problem coz i checked whether the pointers are NULL or not (when performing delete). So, why is there a debug assertion? My class header is #ifndef UIMAGE_H #define UIMAGE_H class uImage { private: int *R; int *G; int *B; public: uImage(); ~uImage(); void Create(int x); }; #endif My Class is #include "StdAfx.h" #include "uImage.h" uImage::uImage() { } uImage::~uImage() { if(R!=NULL) delete R; if(G!=NULL) delete G; if(B!=NULL) delete B; } void uImage::Create(int x) { R=new int[x]; G=new int[x]; B=new int[x]; }

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      uus99 wrote: if(R!=NULL) delete R; if(G!=NULL) delete G; if(B!=NULL) delete B; Should be:

      if(R!=NULL) delete [] R;
      if(G!=NULL) delete [] G;
      if(B!=NULL) delete [] B;

      The differences between delete and delete [] were discussed here about a week ago. Search this forum for that thread.


      "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

      U 1 Reply Last reply
      0
      • D David Crow

        uus99 wrote: if(R!=NULL) delete R; if(G!=NULL) delete G; if(B!=NULL) delete B; Should be:

        if(R!=NULL) delete [] R;
        if(G!=NULL) delete [] G;
        if(B!=NULL) delete [] B;

        The differences between delete and delete [] were discussed here about a week ago. Search this forum for that thread.


        "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

        U Offline
        U Offline
        uus99
        wrote on last edited by
        #3

        Yes, you are right, i realized it. Even after i corrected that, there is still the same problem. Why?

        1 Reply Last reply
        0
        • U uus99

          I made a very simple class, and when the default destructor is called, i get a debug assertion failure. (i'm using VC++ SP5) if i call a function like this void Testfunction() { uImage test; test.Create(100); } the function exits properly, with the default destructor deleting the arrays successfully, however,if i call a function like this, that is without allocating the arrays, void Testfunction() { uImage test; } The function exits with a Debug Assertion Failure, _CrtIsValidHeapPointer What i'm confused is that, i'm trying to do cleanup in the destructor by deleting all the dynamic arrays. Although i havent created the arrays, there should be no problem coz i checked whether the pointers are NULL or not (when performing delete). So, why is there a debug assertion? My class header is #ifndef UIMAGE_H #define UIMAGE_H class uImage { private: int *R; int *G; int *B; public: uImage(); ~uImage(); void Create(int x); }; #endif My Class is #include "StdAfx.h" #include "uImage.h" uImage::uImage() { } uImage::~uImage() { if(R!=NULL) delete R; if(G!=NULL) delete G; if(B!=NULL) delete B; } void uImage::Create(int x) { R=new int[x]; G=new int[x]; B=new int[x]; }

          A Offline
          A Offline
          Antony M Kancidrowski
          wrote on last edited by
          #4

          You have not initialised the pointers.

          uImage::uImage() : R(NULL), G(NULL), B(NULL)
          {

          }

          or

          uImage::uImage()
          {
          R = NULL;
          G = NULL;
          B = NULL;
          }

          Given this you do not need to check for R != NULL etc before calling delete as delete does the check for you. Secondly you have missed deleting the array properly.

          delete [] R;
          R = NULL;

          etc... I would set R = NULL; after the delete also. But that's just me!;) Ant. I'm hard, yet soft.
          I'm coloured, yet clear.
          I'm fuity and sweet.
          I'm jelly, what am I? Muse on it further, I shall return!
          - David Williams (Little Britain)

          U 1 Reply Last reply
          0
          • A Antony M Kancidrowski

            You have not initialised the pointers.

            uImage::uImage() : R(NULL), G(NULL), B(NULL)
            {

            }

            or

            uImage::uImage()
            {
            R = NULL;
            G = NULL;
            B = NULL;
            }

            Given this you do not need to check for R != NULL etc before calling delete as delete does the check for you. Secondly you have missed deleting the array properly.

            delete [] R;
            R = NULL;

            etc... I would set R = NULL; after the delete also. But that's just me!;) Ant. I'm hard, yet soft.
            I'm coloured, yet clear.
            I'm fuity and sweet.
            I'm jelly, what am I? Muse on it further, I shall return!
            - David Williams (Little Britain)

            U Offline
            U Offline
            uus99
            wrote on last edited by
            #5

            Thanks! The initialization of those pointers to Null solved my problem right away! I never knew i had to do that! Thanks again!

            J H D 3 Replies Last reply
            0
            • U uus99

              Thanks! The initialization of those pointers to Null solved my problem right away! I never knew i had to do that! Thanks again!

              J Offline
              J Offline
              Jeremy Falcon
              wrote on last edited by
              #6

              As a rule of thumb, always initialize pointers to null (unless you have a reason to initialize them with a value). When it becomes a habit, It'll make testing against them much easier in the future. Jeremy Falcon

              1 Reply Last reply
              0
              • U uus99

                Thanks! The initialization of those pointers to Null solved my problem right away! I never knew i had to do that! Thanks again!

                H Offline
                H Offline
                Henry miller
                wrote on last edited by
                #7

                You have to do that. Your pointers were not NULL, they were undefined which means any value is legal. My debugger expliactly sets everything to 0xAAAAAAAA to make sure that they are not null. You are lucky, on many systems they will be NULL 95% of the time and you will tear your hair out trying to figgure out why your program crashes one time in 20.

                1 Reply Last reply
                0
                • U uus99

                  Thanks! The initialization of those pointers to Null solved my problem right away! I never knew i had to do that! Thanks again!

                  D Offline
                  D Offline
                  digwizfox
                  wrote on last edited by
                  #8

                  That's interesting. I would think that the proper usage of delete contributed to the solution as well. Both are absolutely critical to ensuring that your code works properly. Not using new and delete properly will cause undefined behavior (sometimes your code will crash, sometimes it won't). I've seen code like this: #include "gconst.h" struct globalmem { int counter; int[SIZE] nDataBuffer; //just as an example, the real data struct had many //other attributes as well } //now later, in some other piece of code globalmem* gpGlobalMem = new globalmem; //Now even later upon application exiting delete[] globalmem; //oops! use delete globalmem //remember, if new was used, delete should be used for //destruction. Always use the same format of new and //delete (even though there is an array in global mem //you still instantiated only 1 globalmem structure //using new. Now what was really weird was that the delete[] globalmem statement was in our code for years, even though it was wrong and never caused problems. But after adding even more variables to the data structure during the enhancement to a system, the software just started crashing when trying to exit the application.

                  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