STL <list> destructor</list>
-
I have a class called subscriber in a project. Several classes inherit from it but as of now, none use the functionality. The destructor is empty but calls the STL destructor. Since the class isn't used yet, the list is empty. This then crashes the program when shutting down because the empty list of pointers creates a null pointer in the destructor. Is it me or aren't empty lists supposed to disappear without trying to empty themselves? Class is below. Mark Jackson class subscriber { public: subscriber() {} subscriber(const subscriber& s); ~subscriber() {} virtual void update() = 0; void subscribe(broadCaster * b); void unSubscribe(broadCaster * b); void unSubscribe(); private: std::list _mBroadList; typedef std::list::iterator iterator; }; // subscriber
-
I have a class called subscriber in a project. Several classes inherit from it but as of now, none use the functionality. The destructor is empty but calls the STL destructor. Since the class isn't used yet, the list is empty. This then crashes the program when shutting down because the empty list of pointers creates a null pointer in the destructor. Is it me or aren't empty lists supposed to disappear without trying to empty themselves? Class is below. Mark Jackson class subscriber { public: subscriber() {} subscriber(const subscriber& s); ~subscriber() {} virtual void update() = 0; void subscribe(broadCaster * b); void unSubscribe(broadCaster * b); void unSubscribe(); private: std::list _mBroadList; typedef std::list::iterator iterator; }; // subscriber
mjackson11 wrote:
because the empty list of pointers creates a null pointer in the destructor
Huh ?? if no one "subscribe" the list will be empty; the size will be zero. if there are subscription, then the list will be non-empty; it's not the job of the subscriber class to delete the broadCaster; so no major cleaning to be done here. in the destructor, I would do (just to be certain)
_mBroadLost.clear();
This signature was proudly tested on animals.
-
I have a class called subscriber in a project. Several classes inherit from it but as of now, none use the functionality. The destructor is empty but calls the STL destructor. Since the class isn't used yet, the list is empty. This then crashes the program when shutting down because the empty list of pointers creates a null pointer in the destructor. Is it me or aren't empty lists supposed to disappear without trying to empty themselves? Class is below. Mark Jackson class subscriber { public: subscriber() {} subscriber(const subscriber& s); ~subscriber() {} virtual void update() = 0; void subscribe(broadCaster * b); void unSubscribe(broadCaster * b); void unSubscribe(); private: std::list _mBroadList; typedef std::list::iterator iterator; }; // subscriber
Duh, careless virtual in a baseclass declaration led to multiple calls to the destructor.
-
I have a class called subscriber in a project. Several classes inherit from it but as of now, none use the functionality. The destructor is empty but calls the STL destructor. Since the class isn't used yet, the list is empty. This then crashes the program when shutting down because the empty list of pointers creates a null pointer in the destructor. Is it me or aren't empty lists supposed to disappear without trying to empty themselves? Class is below. Mark Jackson class subscriber { public: subscriber() {} subscriber(const subscriber& s); ~subscriber() {} virtual void update() = 0; void subscribe(broadCaster * b); void unSubscribe(broadCaster * b); void unSubscribe(); private: std::list _mBroadList; typedef std::list::iterator iterator; }; // subscriber
Using pointers to things as the element type template parameter to STL collections can be somewhat tricky. Is there any reason in particular why you are using a
list<broadCaster *>
instead of justlist<broadCaster>
? With a list of pointers tobroadCaster
objects, maintaining the lifetime of the objects pointed at by those pointers typically becomes an issue. This is just a general guideline though - depending on your specific requirements it might make sense to do this.-- gleat http://blogorama.nerdworks.in[^] --
-
I have a class called subscriber in a project. Several classes inherit from it but as of now, none use the functionality. The destructor is empty but calls the STL destructor. Since the class isn't used yet, the list is empty. This then crashes the program when shutting down because the empty list of pointers creates a null pointer in the destructor. Is it me or aren't empty lists supposed to disappear without trying to empty themselves? Class is below. Mark Jackson class subscriber { public: subscriber() {} subscriber(const subscriber& s); ~subscriber() {} virtual void update() = 0; void subscribe(broadCaster * b); void unSubscribe(broadCaster * b); void unSubscribe(); private: std::list _mBroadList; typedef std::list::iterator iterator; }; // subscriber
BTW, Make your destructor virtual. If you're accessing derived class object though base class point and if you delete it, derived class constructor will not be called. See FAQ[^]
-Sarath. "Great hopes make everything great possible" - Benjamin Franklin
My blog - Sharing My Thoughts