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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. STL <list> destructor</list>

STL <list> destructor</list>

Scheduled Pinned Locked Moved C / C++ / MFC
c++questionannouncement
5 Posts 4 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.
  • M Offline
    M Offline
    mjackson11
    wrote on last edited by
    #1

    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

    M M R S 4 Replies Last reply
    0
    • M mjackson11

      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

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • M mjackson11

        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

        M Offline
        M Offline
        mjackson11
        wrote on last edited by
        #3

        Duh, careless virtual in a baseclass declaration led to multiple calls to the destructor.

        1 Reply Last reply
        0
        • M mjackson11

          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

          R Offline
          R Offline
          Rajasekharan Vengalil
          wrote on last edited by
          #4

          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 just list<broadCaster>? With a list of pointers to broadCaster 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[^] --

          1 Reply Last reply
          0
          • M mjackson11

            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

            S Offline
            S Offline
            Sarath C
            wrote on last edited by
            #5

            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

            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