Thread: need help with destructor

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    7

    need help with destructor

    I have a class named PolyLine with the following data members:

    double *xArray;
    double *yArray;
    int length;

    I have a destructor:

    ~PolyLine()
    {
    cout << "I'm destroying you";
    delete [] xArray;
    delete [] yArray;
    }

    when I delete a PolyLine:

    delete p1;//where p1 is a pointer to a PolyLine

    The text "I'm destroying you" is printed but the object can still be printed. Could someone tell me what I am doing wrong ? Thanks
    Last edited by grimjb; 10-01-2001 at 08:14 PM.

  2. #2
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    Technically the pointer is still pointing to the memory location. Also known as a dangling pointer. It's good practice to set pointers to NULL when done with them. That way you can't accidently mess things up. Also check to make sure it exists before you delete it. This can sometimes cause problems as well.

    Code:
    ~PolyLine() 
    { 
      cout << "I'm destroying you"; 
      if ( xArray )
      {
        delete [] xArray; 
        xArray = NULL;
      }
      if ( yArray )
      {
        delete [] yArray;
        yArray = NULL;
      } 
    }
    Last edited by taylorguitarman; 10-01-2001 at 10:36 PM.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    11
    Grim, your question looks familiar. Oh yeah, I've asked it before.

    I just wanted to add something as clarification. When you delete some memory, the memory may not necessarily be cleaned as soon as you delete it. So, basically as far as the OS is concerned, the value is the previously allocated memory is garbage. That garbage could be cleaned out tomorrow, two minutes from now, whatever. It can also be overwritten.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    According to Bruce Eckel in Thinking in C++ "If the pointer you're deleting is zero, nothing will happen. For this reason, people often recommend setting a pointer to zero immediately after you delete it...".

    And more importantly Sroustrap says "The delete [] operator may be applied only to a pointer to an array returned by new or to zero. Applying delete[] to zero has no effect."

    The only reason I point this out is I used to think that also until it was pointed out to me.

  5. #5
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    Nothing more to say, but the freed memory area may be overwritten when the program makes new allocations with new or new []. So if you still have the old pointer which was deleted pointing to this same address that is recently allocated for something else, and you use this old pointer, something terrible may happen.
    Making error is human, but for messing things thoroughly it takes a computer

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    7

    thanks guys... one more question...

    I tried to print the PolyLine after the new and improved destructor and I can honestly say that I've never been to happy to see a segmentation fault =)

    Could you guys answer another question for me ?


    PolyLine *p1 = new PolyLine(x, y, 3); //dynamic constructor
    PolyLine *p2 (p1);//copy constructor

    I want the second line to look like PolyLine p2(p1); ie: I want p2 to be a reference not a pointer. I want p2 to be constructed with the following method (can be modified if need be).

    btw: datamembers for PolyLine are:
    private:
    double *xArray;
    double *yArray;
    int length;

    PolyLine(const PolyLine& src)
    {
    int i;
    double x1, x2;
    cout << "inside deep copy\n";
    for(i=0;i<src.length;i++)
    {
    x1 = src.xArray[i];
    xArray[i] = x1;
    x2 = src.yArray[i];
    yArray[i] = x2;
    }
    length = src.length;
    }

    I am not sure what happens when:

    PolyLine *p2 (p1);//copy constructor

    is executed.. p2 points at p1 and when I destroy p1, p2 gets destroyed too but I don't know what code is making p2 point at p1. Is this something built in to C++? I don't think it's any of my methods doing it. This has had me stumpped for some time.

    I have tried changing the statement to this:

    PolyLine &p2 (p1);//copy constructor

    and I got this error:

    conversion from `PolyLine *' to non-scalar type `PolyLine' requested
    a1.cc:14: cannot initialize `PolyLine &' from `PolyLine *'

    and I tried changing the statement to this:

    PolyLine p2 (p1);//copy constructor

    and got the following error:

    no matching function for call to `PolyLine::PolyLine (PolyLine *&)'
    PolyLine.h:10: candidates are: PolyLine::PolyLine(double *, double *, int)
    PolyLine.h:24: PolyLine::PolyLine(const PolyLine &)

    Anyone know what I'm doing wrong ?
    Thanks

  7. #7
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    If I understand you correctly, then you want to create a PolyLine called p2 by copying the object pointed to by p1?

    If this is the case, then try this:
    Code:
    PolyLine p2(*p1);
    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    7

    That works, but...

    I am writing this as part of an assignment and the assignment says that the method should be called using the following statement:

    PolyLine p2 (p1);

    I think I have to change my method signature but I'm not entirely sure what I should change it to.

    Thanks a lot for your help, that's a big step in the right direction.

  9. #9
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    I am writing this as part of an assignment and the assignment says that the method should be called using the following statement:

    PolyLine p2 (p1);
    Then you'll need a copy constructor that takes a pointer to a PolyLine -

    Code:
    #include<iostream> 
    
    using namespace std;
    
    
    class test
    {
    	int a;
    public:
    	void Print()const{cout << a << endl;}
    	test(const test &rhs){cout << "Copy Constructor 1 Called\n";a=rhs.a;}
    	test(const test* &rhs){cout << "CopyConstructor 2 Called\n";a=rhs->a;}
    	test(int b):a(b){}
    	~test(){cout << "dead\n";}
    };
    
    int main()
    {
    	test* ptr = new test(10);
    	test newTest(ptr);
    	delete ptr;
    	newTest.Print();
    
    	return 0;
    }
    zen

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    7
    ok my new method looks like this:

    PolyLine(const PolyLine* &src)
    { //cout << "inside deep copy\n";
    int i;
    double x1, x2;


    xArray = new double[src->length];
    yArray = new double[src->length];

    for(i=0;i<src->length;i++)
    {
    x1 = src->xArray[i];
    xArray[i] = x1;
    x2 = src->yArray[i];
    yArray[i] = x2;
    }
    length = src->length;
    }

    I envoke it with this statement in main:

    PolyLine p2 (p1);

    but I recieve this compiler error :

    a1.cc: In function `int main()':
    a1.cc:16: initializing non-const `const PolyLine *&' with `PolyLine *' will use a temporary
    PolyLine.h:52: in passing argument 1 of `PolyLine::PolyLine(const PolyLine *&)'

    I'm not sure what this error means and I haven't a clue as to how to fix it =(

  11. #11
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    My fault, it compiles on MSVC (even though it shouldn't). You can fix it on by either removing the const modifier or just have the constructor accept a pointer rather than a reference to pointer.
    zen

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    7

    that did the trick =)

    Thanks everyone for giving a C++ newbie a hand. This is a great forum! you guys rock.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Destructor being called on SGI hash_map key
    By cunnus88 in forum C++ Programming
    Replies: 4
    Last Post: 02-11-2009, 12:05 AM
  2. exception in the destructor
    By coletek in forum C++ Programming
    Replies: 3
    Last Post: 01-12-2009, 12:01 PM
  3. Replies: 1
    Last Post: 06-10-2008, 08:38 PM
  4. Destructor inaccessible
    By renanmzmendes in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2008, 11:07 AM
  5. destructor question
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 03-10-2006, 11:29 AM