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. CPtrList

CPtrList

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

    Hi, I am trying to iterate and delete the elements (assuming atleast 1 element) in a CptrList with this: void *pPt = NULL; POSITION pos = NULL; for (pos = m_points.GetHeadPosition(); !m_points.IsEmpty(); m_points.GetNext(pos)) { // TRACE("pos: %d, %d\n", pos, NULL); pPt = m_points.GetAt(pos); m_points.RemoveAt(pos); delete pPt; } TRACE shows that pos is not equal to NULL as it should be after the last element has been seen. So pos != NULL check does not work contrary to the MSDN Library documentation. THIS IS ONE PROBLEM. The OTHER is even if there are more than 1 elements the second element returned by GetAt is not "valid" (0xcdcdcdcd) and RemoveAt asserts. Any clues? Regards Mahendra

    N G 2 Replies Last reply
    0
    • M Mahendra_786

      Hi, I am trying to iterate and delete the elements (assuming atleast 1 element) in a CptrList with this: void *pPt = NULL; POSITION pos = NULL; for (pos = m_points.GetHeadPosition(); !m_points.IsEmpty(); m_points.GetNext(pos)) { // TRACE("pos: %d, %d\n", pos, NULL); pPt = m_points.GetAt(pos); m_points.RemoveAt(pos); delete pPt; } TRACE shows that pos is not equal to NULL as it should be after the last element has been seen. So pos != NULL check does not work contrary to the MSDN Library documentation. THIS IS ONE PROBLEM. The OTHER is even if there are more than 1 elements the second element returned by GetAt is not "valid" (0xcdcdcdcd) and RemoveAt asserts. Any clues? Regards Mahendra

      N Offline
      N Offline
      Navin
      wrote on last edited by
      #2

      As soon as you remove the element, you invalidate the position. Here's a better way of doing it: { void *pPt = NULL; POSITION pos = NULL; for (pos = m_points.GetHeadPosition(); pos != NULL; m_points.GetNext(pos)) { // TRACE("pos: %d, %d\n", pos, NULL); pPt = m_points.GetAt(pos); delete pPt; } // Now we have a list full of invalid pointers, so remove them all m_points.RemoveAll(); } "Fish and guests stink in three days." - Benjamin Franlkin

      M 1 Reply Last reply
      0
      • N Navin

        As soon as you remove the element, you invalidate the position. Here's a better way of doing it: { void *pPt = NULL; POSITION pos = NULL; for (pos = m_points.GetHeadPosition(); pos != NULL; m_points.GetNext(pos)) { // TRACE("pos: %d, %d\n", pos, NULL); pPt = m_points.GetAt(pos); delete pPt; } // Now we have a list full of invalid pointers, so remove them all m_points.RemoveAll(); } "Fish and guests stink in three days." - Benjamin Franlkin

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

        But is this ok - (re)moving from the tail end if a sub-sequence has to be removed and RQUIRES using RemoveAt. This seems to works: for (pos = m_points.GetTailPosition(); !m_points.IsEmpty(); m_points.GetPrev(pos)) { pPt = m_points.GetAt(pos); delete pPt; m_points.RemoveAt(pos); } pos is still not NULL in the end though! Or like in the MSDNL RemoveAt help!: POSITION pos1 = NULL; POSITION pos2 = NULL; for (pos1 = m_points.GetHeadPosition(); (pos2 = pos1) != NULL; ) { m_points.GetNext(pos1); pPt = m_points.GetAt(pos2); delete pPt; m_points.RemoveAt(pos2); } Due Regards Mahendra

        1 Reply Last reply
        0
        • M Mahendra_786

          Hi, I am trying to iterate and delete the elements (assuming atleast 1 element) in a CptrList with this: void *pPt = NULL; POSITION pos = NULL; for (pos = m_points.GetHeadPosition(); !m_points.IsEmpty(); m_points.GetNext(pos)) { // TRACE("pos: %d, %d\n", pos, NULL); pPt = m_points.GetAt(pos); m_points.RemoveAt(pos); delete pPt; } TRACE shows that pos is not equal to NULL as it should be after the last element has been seen. So pos != NULL check does not work contrary to the MSDN Library documentation. THIS IS ONE PROBLEM. The OTHER is even if there are more than 1 elements the second element returned by GetAt is not "valid" (0xcdcdcdcd) and RemoveAt asserts. Any clues? Regards Mahendra

          G Offline
          G Offline
          Gary R Wheeler
          wrote on last edited by
          #4

          One problem I see in your example is the line delete pPt;. This line won't work correctly. Since pPt is a void pointer, the compiler won't know which delete operator and destructor to call. This will lead to memory leaks and possibly heap corruption. A better approach would be:

          while (!m_points.IsEmpty()) {
          MyType *pPt = (MyType *)m_points.RemoveHead();
          delete pPt;
          }

          In this case, MyType is the type of the value pointed to by members of the list. If you use a CTypedPtrList<>, you can delete the contents of a list more simply as follows:

          CTypedPtrList<MyType> m_points;
          //...
          while (!m_points.IsEmpty()) {
          delete m_points.RemoveHead();
          }

          Using CTypedPtrList you don't need type casts.


          Software Zen: delete this;

          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