delete operator in c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mickey22
    New Member
    • Feb 2007
    • 105

    delete operator in c++

    Hi all,

    I have a class say A and created a pointer to call this class like

    A *obj=NULL;
    obj=new A( );

    After I perform all the operations within the code and try to delete this pointer using

    delete obj;
    obj=NULL;

    I get an exception as
    " the instruction at "0x7c910f 2b" REFERENCED MEMORY AT "0x00000004".th e memory could not be "read".Clic k OK to terminate the program.

    I am not able to figure it out.

    If i dont delete it the code runs smoothly.But always have to delete after use once allocated.The whole code is within a function block.

    Could anyone please suggest me.

    Thanks .
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Try removing the obj=NULL; portion and see if that works?

    Comment

    • sicarie
      Recognized Expert Specialist
      • Nov 2006
      • 4677

      #3
      Originally posted by mickey22
      delete obj;
      obj=NULL;
      So you're deleting obj and then trying to assign it a NULL value?

      Comment

      • Cucumber
        New Member
        • Sep 2007
        • 90

        #4
        Post the code of the destructor.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Have you stepped through your destructor with your debugger??

          Comment

          • mickey22
            New Member
            • Feb 2007
            • 105

            #6
            I removed NULL. It did not work.

            I have my code in this way:

            my own class

            [CODE=cpp]///abc.h

            class abc
            {
            int off;
            int *img;
            abc(void);
            ~abc( );
            void sum( );
            };

            //abc.cpp

            abc::abc(void)
            {
            off=0;
            }
            abc::~abc( )
            {
            delete[] img;
            }

            void abc::sum( )
            {
            Filter *f; //class already created by someone and I am using all its functions
            f=new Filter ( );
            std::string filename=f->datafile();
            raw *r; //another class same like f created by someone.
            r=new raw( );
            r->add(filename );
            img=new float[100];
            delete r;
            r=NULL;
            delete f;
            f=NULL;
            }[/CODE]



            I am getting that exception just before add function .I get the filename correctly from the class but when I pass to the add( ) that filename is corrupted and I see it as Bad Ptr when I stepped in thru the debugger step by step. when I delete r, I dont get any exception.

            It is little bit confusing.Pleas e let me now if any questions.I am still trying to figure it out.
            Last edited by Ganon11; Nov 6 '07, 06:32 PM. Reason: Please use the [CODE] tags provided.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Originally posted by Mickey22
              Filter *f; //class already created by someone and I am using all its functions
              f=new Filter ( );
              This should be:
              [code=cpp]
              Filter *f; //class already created by someone and I am using all its functions
              f=new Filter;
              [/code]

              Also, your abc class:
              Originally posted by Mickey22
              class abc
              {
              int off;
              int *img;
              abc(void);
              ~abc( );
              void sum( );
              };
              Has no constructor. Therefore no guarantee that the img pointer ever got initialized. If you delete and uninitialized pointer, you crash.

              Comment

              • RobRussell
                New Member
                • Nov 2007
                • 6

                #8
                The best way to find it is to start by adding error checking. For example:
                [CODE=cpp]
                abc::abc()
                {
                off=0;
                img = NULL;
                }
                ...
                abc::~abc()
                {
                if (img) {
                delete(img);
                }
                img = NULL;
                }
                [/CODE]

                Adding the code will benefit you in the long run because it will catch the problem every time - even if it shouldn't happen any more.

                I'm pretty sure that
                [CODE=cpp]
                std::string filename=f->datafile();
                [/CODE]
                copies the return value of datafile() then it gets copied again on the way in to add(). If you meant to get a pointer or a reference then make sure that's what's happening. For example, a pointer allocated on the stack inside datafile() won't be valid after that function returns. I'm always fuzzy on these things in C++ though.

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  Originally posted by RobRussell
                  The best way to find it is to start by adding error checking. For example:

                  Code: ( cpp )
                  abc::abc()
                  {
                  off=0;
                  img = NULL;
                  }
                  ...
                  abc::~abc()
                  {
                  if (img) { <<<<<<<<<<<<<<< <<<<<<<<<<<
                  delete(img);
                  }
                  img = NULL;
                  }
                  Pointless test. delete does nothing if the pointer is null. However, setting the pointer to null after the delete is a good idea. This is different from C. There, free() crashes on a null pointer.

                  Originally posted by RobRussell
                  For example, a pointer allocated on the stack inside datafile() won't be valid after that function returns. I'm always fuzzy on these things in C++ though.
                  It's not the pointer that's the problem. It's the address inside the pointer. If that address is to another local variable, then you have a problem. But if the address is to something on the heap, then that address will be valid until you delete it.

                  Comment

                  • RRick
                    Recognized Expert Contributor
                    • Feb 2007
                    • 463

                    #10
                    Rob Russell had the right idea by setting img = NULL in the constructor. This is what was missing in the original constructor. When the object was created, img had a non-zero value which caused delete to crash and burn.

                    A minor point, in C++, NULL has fallen out of favor. Using img = 0 is preferable.

                    Comment

                    Working...