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. ATL / WTL / STL
  4. keeping track of pointers to objects with STL

keeping track of pointers to objects with STL

Scheduled Pinned Locked Moved ATL / WTL / STL
c++questioncomtutoriallearning
3 Posts 3 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.
  • J Offline
    J Offline
    Jeremy Pullicino
    wrote on last edited by
    #1

    I have an stl list of pointers to objects. When I need to insert an object to the list, I new it and push_back its pointer. When the list is not needed any more, I iterate through the entire list and I delete each pointer. First question: Is there a more elegant way to do this or is my implementation ok? Sometimes I wish to copy a pointer for one list into another list, meaning that the two lists will contain a pointer to the same object. When the first list is destroyed, I delete all pointers inside it, as mentioned above. When the second list is destroyed, I do the same, however my pointers are already deleted and this of course crashes the program. My first thought was to implement reference counting on my object, however I am unsure of where to increment the reference. The code looks something like this: std::list list1; std::list list2; CMyObj* pOBJ = new CMyObj; list1.push_back(pOBJ); list2.insert(list2.begin(), list1.begin(), list1.end()); // delete all elements in list1 // delete all elements in list2 (crash) When list2.insert is called, a copy of the pointer from list1 is inserted into list2. I will need to increment the reference of the pointer which is inserted, however I am unsure of how to do this. Does anyone have a solution to this? Thanks in advance, Jeremy Pullicino C++ Developer Homepage

    J N 2 Replies Last reply
    0
    • J Jeremy Pullicino

      I have an stl list of pointers to objects. When I need to insert an object to the list, I new it and push_back its pointer. When the list is not needed any more, I iterate through the entire list and I delete each pointer. First question: Is there a more elegant way to do this or is my implementation ok? Sometimes I wish to copy a pointer for one list into another list, meaning that the two lists will contain a pointer to the same object. When the first list is destroyed, I delete all pointers inside it, as mentioned above. When the second list is destroyed, I do the same, however my pointers are already deleted and this of course crashes the program. My first thought was to implement reference counting on my object, however I am unsure of where to increment the reference. The code looks something like this: std::list list1; std::list list2; CMyObj* pOBJ = new CMyObj; list1.push_back(pOBJ); list2.insert(list2.begin(), list1.begin(), list1.end()); // delete all elements in list1 // delete all elements in list2 (crash) When list2.insert is called, a copy of the pointer from list1 is inserted into list2. I will need to increment the reference of the pointer which is inserted, however I am unsure of how to do this. Does anyone have a solution to this? Thanks in advance, Jeremy Pullicino C++ Developer Homepage

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      Hi Jeremy, As you correctly point out, some sort of ref counting is needed to address your problem. IMHO providing CMyObj with the ref counting machinery is not a good idea as this artifact properly does not belong to CMyObj semantics, but rather it is imposed by external conditions (the necessity of storing CMyObj pointers into several containers). A much cleaner approach is to use some sort of smart pointer that does the ref counting to the objects pointed to and deletes them when the objects are no further referenced. If you feel comfortable with Boost, check Boost Smart Pointers Library[^]: in particular, boost::shared_ptr does exactly what you need. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      1 Reply Last reply
      0
      • J Jeremy Pullicino

        I have an stl list of pointers to objects. When I need to insert an object to the list, I new it and push_back its pointer. When the list is not needed any more, I iterate through the entire list and I delete each pointer. First question: Is there a more elegant way to do this or is my implementation ok? Sometimes I wish to copy a pointer for one list into another list, meaning that the two lists will contain a pointer to the same object. When the first list is destroyed, I delete all pointers inside it, as mentioned above. When the second list is destroyed, I do the same, however my pointers are already deleted and this of course crashes the program. My first thought was to implement reference counting on my object, however I am unsure of where to increment the reference. The code looks something like this: std::list list1; std::list list2; CMyObj* pOBJ = new CMyObj; list1.push_back(pOBJ); list2.insert(list2.begin(), list1.begin(), list1.end()); // delete all elements in list1 // delete all elements in list2 (crash) When list2.insert is called, a copy of the pointer from list1 is inserted into list2. I will need to increment the reference of the pointer which is inserted, however I am unsure of how to do this. Does anyone have a solution to this? Thanks in advance, Jeremy Pullicino C++ Developer Homepage

        N Offline
        N Offline
        Nemanja Trifunovic
        wrote on last edited by
        #3

        I highly recommend Loki::SmartPtr[^] for that purpose. Or if you don't feel comfortable with this, use boost::shared_ptr.

        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