Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #2996

Destructors can copying objects

From Ruben Safir <ruben@mrbrklyn.com>
Newsgroups comp.lang.c++
Subject Destructors can copying objects
Date 2011-04-02 12:58 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <in76i3$105$2@reader1.panix.com> (permalink)

Show all headers | View raw


I am creating a linked link program which created a segmentation fault
and a core at the end of the program. The program is trying to delete an
already deleted object, and I can't seem to protect my program from
this. I fixed the problem by sending an object to a function by
reference rather than by value, but I'm not satisfied with the solution.

The list has node objects

namespace chainlist {


/*
*
=====================================================================================
* Class: Node
* Description: This is a node in the "List" Link List
*
=====================================================================================
*/

template < class unk >
class Node
{
public:

// ==================== LIFECYCLE =======================================
/* constructor */
Node<unk>( unk value, Node<unk> *item_to_link_to = 0);
Node ( const Node &other ); /* copy constructor - Need to write this! */
~Node<unk> (); /* destructor */


/* ==================== ACCESSORS ======================================= */
inline unk* value ()const{
return value_;
}
inline Node<unk> * next()const {
return next_;
}

inline void value(unk *);
inline void value(unk);
inline void next(Node<unk> *);





/* ==================== MUTATORS ======================================= */

/* ==================== OPERATORS ======================================= */


Node& operator=( const Node &other ); // assignment operator
unk& operator*(){ //gain access to real value
return *(value());
}
unk& operator*(unk);

protected:
/* ==================== DATA MEMBERS
======================================= */



private:
/* ==================== DATA MEMBERS
======================================= */

unk * value_ ;
Node * next_;



}; /* ----- end of template class Node ----- */



And they are controlled by a List Object


template < class T >
class List
{
public:

/* ==================== LIFECYCLE ======================================= */
List<T>(): front_(0),end_(0), cursor_(0), size_(0){} /* constructor */
void remove_all();
~List<T>();

/* ==================== ACCESSORS ======================================= */
int size()const { return size_; }
inline Node<T>* const &front()const{ return front_;}
inline Node<T>* &endd(){return end_;}

inline Node<T> *& cursor(){return cursor_;}

inline void cursor(Node<T> * a){ cursor_ = a; }

inline void endd(Node<T> *a){ end_ = a; }
inline void front(Node<T> *a){ front_ = a; }
inline void display();



/* ==================== MUTATORS ======================================= */

inline void sizeup(){ ++size_; }
inline void sizedown(){ --size_; }
void insert_front( T value); //add a node to the front
void insert(T value, Node<T> * &place_after_this); //add a new node
after the Node given and return the new node
void insert(T value); //add a new node at the end of the list
void insert_end(T value); // Add a new node to the end

void find_value(T value); //find a value from it's first occurance in
the list
Node<T>& find_next_value(const T& value, Node<T>& last_node_searched);
//find a value in the list after the
//current node which is not searched
void remove_node_by_value(T value);
void remove_node_by_node(Node<T> cur);
void remove_node_front();
void remove_node_end();



/* ==================== OPERATORS ======================================= */

protected:
/* ==================== DATA MEMBERS
======================================= */

private:
/* ==================== DATA MEMBERS
======================================= */
Node<T> * front_;
Node<T> * end_;
Node<T> * cursor_;
int size_;

}; /* ---------- end of template class Lists ---------- */


The destructors are defined as follows

template<class unk>
Node<unk>::~Node(){
if(value_)
delete value_;
std::cout << "Delete Node" << std::endl;

}

template<class T>
List<T>::~List<T>(){
remove_all();
std::cout << "Deleted All*************" << std::endl;
}



And remove all looks like this
template< class T>
void List<T>::remove_all(){
cursor() = front();
Node<T> * tmp;
while(cursor() != endd()){
tmp = cursor();
cursor() = cursor()->next();
if(tmp)
delete tmp;
}
std::cout << "\n\nReached the End\n\n";
tmp = cursor(); //last node
if(tmp)
delete tmp;
cursor() = 0; //park the cursor
}


I created a large list to test my find functions and passed it by value
to main.
Then passed it by value to a find testing function. when the program ends,
it seems to delete all the nodes from the List twice, once from the local
program and then from main.

Sending it by reference fixed the problem, but I can see other instances
where this can be a huge problem, such as if I decide o delete an
individual node from the list. There has to be a better strategy for
handling this issue.
~

Back to comp.lang.c++ | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-02 12:58 +0000
  Re: Destructors can copying objects Volker Lukas <vlukas@gmx.de> - 2011-04-02 15:29 +0200
    Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-03 02:59 +0000
      Re: Destructors can copying objects "Balog Pal" <pasa@lib.hu> - 2011-04-03 11:32 +0200
        Re: Destructors can copying objects Leigh Johnston <leigh@i42.co.uk> - 2011-04-03 12:29 +0100
          Re: Destructors can copying objects Bart van Ingen Schenau <bart@ingen.ddns.info> - 2011-04-04 13:43 +0200
            Re: Destructors can copying objects Michael Doubez <michael.doubez@free.fr> - 2011-04-05 01:26 -0700
          Re: Destructors can copying objects Joshua Maurice <joshuamaurice@gmail.com> - 2011-04-04 14:03 -0700
            Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 21:10 +0000
            Re: Destructors can copying objects Leigh Johnston <leigh@i42.co.uk> - 2011-04-04 22:28 +0100
          Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 21:07 +0000
            Re: Destructors can copying objects Leigh Johnston <leigh@i42.co.uk> - 2011-04-04 22:31 +0100
              Re: Destructors can copying objects Leigh Johnston <leigh@i42.co.uk> - 2011-04-04 22:43 +0100
              Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 22:38 +0000
                Re: Destructors can copying objects Leigh Johnston <leigh@i42.co.uk> - 2011-04-04 23:42 +0100
                Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 22:54 +0000
      Re: Destructors can copying objects James Kanze <james.kanze@gmail.com> - 2011-04-03 10:07 -0700
        Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 21:14 +0000
        Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 21:17 +0000
          Re: Destructors can copying objects LR <lruss@superlink.net> - 2011-04-05 01:52 -0400
    Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-03 03:08 +0000
      Re: Destructors can copying objects Paavo Helde <myfirstname@osa.pri.ee> - 2011-04-03 02:11 -0500
      Re: Destructors can copying objects Volker Lukas <vlukas@gmx.de> - 2011-04-03 12:02 +0200
      Re: Destructors can copying objects Peter Remmers <p.remmers@expires-2011-04-30.arcornews.de> - 2011-04-03 12:14 +0200
        Re: Destructors can copying objects Roberto Waltman <usenet@rwaltman.com> - 2011-04-03 18:16 -0400
          Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-15 23:06 +0000
            Re: Destructors can copying objects LR <lruss@superlink.net> - 2011-04-15 19:19 -0400
        Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-15 23:03 +0000
          Re: Destructors can copying objects "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-04-16 01:47 +0200
            Re: Destructors can copying objects Peter Remmers <p.remmers@expires-2011-04-30.arcornews.de> - 2011-04-16 13:00 +0200
              Re: Destructors can copying objects "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-04-16 15:57 +0200
  Re: Destructors can copying objects LR <lruss@superlink.net> - 2011-04-02 09:51 -0400
  Re: Destructors can copying objects Yannick Tremblay <yannick@tremnblay.co.uk> - 2011-04-04 12:56 +0000
    Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-04 21:05 +0000
      Re: Destructors can copying objects Paavo Helde <myfirstname@osa.pri.ee> - 2011-04-04 21:56 -0500
      Re: Destructors can copying objects Yannick Tremblay <yannick@tremnblay.co.uk> - 2011-04-05 11:36 +0000
        Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-05 15:40 +0000
          Re: Destructors can copying objects Sana <sanatakos@gmail.com> - 2011-04-05 10:11 -0700
          Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-05 20:28 +0000
          Re: Destructors can copying objects Yannick Tremblay <yannick@tremnblay.co.uk> - 2011-04-06 11:12 +0000
      Re: Destructors can copying objects drew@furrfu.invalid (Drew Lawson) - 2011-04-05 13:02 +0000
        Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-05 15:51 +0000
          Re: Destructors can copying objects drew@furrfu.invalid (Drew Lawson) - 2011-04-05 17:47 +0000
            Re: Destructors can copying objects Ruben Safir <ruben@mrbrklyn.com> - 2011-04-05 20:25 +0000
              Re: Destructors can copying objects drew@furrfu.invalid (Drew Lawson) - 2011-04-05 20:37 +0000

csiph-web