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. Other Discussions
  3. Clever Code
  4. Working with STL-Vector

Working with STL-Vector

Scheduled Pinned Locked Moved Clever Code
debuggingc++graphicsperformancehelp
14 Posts 9 Posters 3 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
    Mr Brainley
    wrote on last edited by
    #1

    I wrote this :

    while (!(m\_WorkerThreadList.empty()))
    {
    	for (	it = m\_WorkerThreadList.begin(); 
    			it != m\_WorkerThreadList.end();
    			it++)
    		if (!((\*it)->isAlive()))
    		{
    			delete (\*it);
    			it	= m\_WorkerThreadList.erase(it);
    		}
    }
    

    I found, that not all elements are actually deleted. The programm did not crash though, unless i used the debugger. If i just let the Debug build run, things where fine, except for a memory leak. If i used a debugger, the programm chrashed due to memory violation. However, that's not the problem here. It's a classic for loop thing.

    L C P S R 5 Replies Last reply
    0
    • M Mr Brainley

      I wrote this :

      while (!(m\_WorkerThreadList.empty()))
      {
      	for (	it = m\_WorkerThreadList.begin(); 
      			it != m\_WorkerThreadList.end();
      			it++)
      		if (!((\*it)->isAlive()))
      		{
      			delete (\*it);
      			it	= m\_WorkerThreadList.erase(it);
      		}
      }
      

      I found, that not all elements are actually deleted. The programm did not crash though, unless i used the debugger. If i just let the Debug build run, things where fine, except for a memory leak. If i used a debugger, the programm chrashed due to memory violation. However, that's not the problem here. It's a classic for loop thing.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Yes, removing items from a container whilst iterating through it is probably a bad idea! :) If necessary, I tend to add the elements I want to keep to a second vector, and then overwrite the original with the second. Depends on the vector contents though - being the STL, there are no doubt a number of ways you can do this!


      Kicking squealing Gucci little piggy.

      J 1 Reply Last reply
      0
      • M Mr Brainley

        I wrote this :

        while (!(m\_WorkerThreadList.empty()))
        {
        	for (	it = m\_WorkerThreadList.begin(); 
        			it != m\_WorkerThreadList.end();
        			it++)
        		if (!((\*it)->isAlive()))
        		{
        			delete (\*it);
        			it	= m\_WorkerThreadList.erase(it);
        		}
        }
        

        I found, that not all elements are actually deleted. The programm did not crash though, unless i used the debugger. If i just let the Debug build run, things where fine, except for a memory leak. If i used a debugger, the programm chrashed due to memory violation. However, that's not the problem here. It's a classic for loop thing.

        C Offline
        C Offline
        Chris Meech
        wrote on last edited by
        #3

        How can a 'dead' object respond to the function call isAlive(). Seems superflous. :-D

        Chris Meech I am Canadian. [heard in a local bar] Nobody likes jerks. [espeir] Hey, I am part of a special bread, we are called smart people [Captain See Sharp] The zen of the soapbox is hard to attain...[Jörgen Sigvardsson] I wish I could remember what it was like to only have a short term memory.[David Kentley]

        M 1 Reply Last reply
        0
        • C Chris Meech

          How can a 'dead' object respond to the function call isAlive(). Seems superflous. :-D

          Chris Meech I am Canadian. [heard in a local bar] Nobody likes jerks. [espeir] Hey, I am part of a special bread, we are called smart people [Captain See Sharp] The zen of the soapbox is hard to attain...[Jörgen Sigvardsson] I wish I could remember what it was like to only have a short term memory.[David Kentley]

          M Offline
          M Offline
          Mr Brainley
          wrote on last edited by
          #4

          Chris Meech wrote:

          How can a 'dead' object respond to the function call isAlive()

          It's really simple you know - just include voodoo.h and derive from class LivingDead. But before you run the application be shure to put a circle of chalk around your PC and Monitor.

          C 1 Reply Last reply
          0
          • M Mr Brainley

            I wrote this :

            while (!(m\_WorkerThreadList.empty()))
            {
            	for (	it = m\_WorkerThreadList.begin(); 
            			it != m\_WorkerThreadList.end();
            			it++)
            		if (!((\*it)->isAlive()))
            		{
            			delete (\*it);
            			it	= m\_WorkerThreadList.erase(it);
            		}
            }
            

            I found, that not all elements are actually deleted. The programm did not crash though, unless i used the debugger. If i just let the Debug build run, things where fine, except for a memory leak. If i used a debugger, the programm chrashed due to memory violation. However, that's not the problem here. It's a classic for loop thing.

            P Offline
            P Offline
            Phil J Pearson
            wrote on last edited by
            #5

            This is supposed to be about subtle bugs!

            Phil


            The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.

            J 1 Reply Last reply
            0
            • M Mr Brainley

              Chris Meech wrote:

              How can a 'dead' object respond to the function call isAlive()

              It's really simple you know - just include voodoo.h and derive from class LivingDead. But before you run the application be shure to put a circle of chalk around your PC and Monitor.

              C Offline
              C Offline
              Chris Meech
              wrote on last edited by
              #6

              LOL. :-D You know I have some apps that have included voodoo.h. It seems they behave very strangely at times. Wonder why?. :cool:

              Chris Meech I am Canadian. [heard in a local bar] Nobody likes jerks. [espeir] Hey, I am part of a special bread, we are called smart people [Captain See Sharp] The zen of the soapbox is hard to attain...[Jörgen Sigvardsson] I wish I could remember what it was like to only have a short term memory.[David Kentley]

              J 1 Reply Last reply
              0
              • L Lost User

                Yes, removing items from a container whilst iterating through it is probably a bad idea! :) If necessary, I tend to add the elements I want to keep to a second vector, and then overwrite the original with the second. Depends on the vector contents though - being the STL, there are no doubt a number of ways you can do this!


                Kicking squealing Gucci little piggy.

                J Offline
                J Offline
                Johann Gerell
                wrote on last edited by
                #7

                Rob Caldecott wrote:

                Yes, removing items from a container whilst iterating through it is probably a bad idea!

                Not at all! Just remember where the iterations should continue: vector<>.erase() gives the next iterator.

                -- The Blog: Bits and Pieces

                L 1 Reply Last reply
                0
                • J Johann Gerell

                  Rob Caldecott wrote:

                  Yes, removing items from a container whilst iterating through it is probably a bad idea!

                  Not at all! Just remember where the iterations should continue: vector<>.erase() gives the next iterator.

                  -- The Blog: Bits and Pieces

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Oops! Of course it does! I totally forgot. However, I still prefer to use std::remove or std::remove_if over hand-crafted loops, to avoid potential issues like this. In the case of the code sample shown, I would also be using a vector of smart pointers, rendering the delete call redundant. I prefer to use algorithms or std::for_each wherever possible.


                  Kicking squealing Gucci little piggy.
                  The Rob Blog

                  1 Reply Last reply
                  0
                  • M Mr Brainley

                    I wrote this :

                    while (!(m\_WorkerThreadList.empty()))
                    {
                    	for (	it = m\_WorkerThreadList.begin(); 
                    			it != m\_WorkerThreadList.end();
                    			it++)
                    		if (!((\*it)->isAlive()))
                    		{
                    			delete (\*it);
                    			it	= m\_WorkerThreadList.erase(it);
                    		}
                    }
                    

                    I found, that not all elements are actually deleted. The programm did not crash though, unless i used the debugger. If i just let the Debug build run, things where fine, except for a memory leak. If i used a debugger, the programm chrashed due to memory violation. However, that's not the problem here. It's a classic for loop thing.

                    S Offline
                    S Offline
                    Stephen Hewitt
                    wrote on last edited by
                    #9

                    The reason is simple. The it++ in the for loop should not be present if the it = m_WorkerThreadList.erase(it); line is executed as if you do both you've skipped two elements not one!

                    Steve

                    D 1 Reply Last reply
                    0
                    • C Chris Meech

                      LOL. :-D You know I have some apps that have included voodoo.h. It seems they behave very strangely at times. Wonder why?. :cool:

                      Chris Meech I am Canadian. [heard in a local bar] Nobody likes jerks. [espeir] Hey, I am part of a special bread, we are called smart people [Captain See Sharp] The zen of the soapbox is hard to attain...[Jörgen Sigvardsson] I wish I could remember what it was like to only have a short term memory.[David Kentley]

                      J Offline
                      J Offline
                      Jorgen Sigvardsson
                      wrote on last edited by
                      #10

                      Have you by any chance worked for 3dfx? ;)

                      -- No humans were probed in the making of this episode

                      1 Reply Last reply
                      0
                      • P Phil J Pearson

                        This is supposed to be about subtle bugs!

                        Phil


                        The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.

                        J Offline
                        J Offline
                        Jorgen Sigvardsson
                        wrote on last edited by
                        #11

                        It's subtle for beginners of STL who aren't quite familiar with iterators, while for those who are, it's one of those "got burned once, and learned from it"-bugs.

                        -- Secreted by the Comedy Bee

                        P 1 Reply Last reply
                        0
                        • J Jorgen Sigvardsson

                          It's subtle for beginners of STL who aren't quite familiar with iterators, while for those who are, it's one of those "got burned once, and learned from it"-bugs.

                          -- Secreted by the Comedy Bee

                          P Offline
                          P Offline
                          Phil J Pearson
                          wrote on last edited by
                          #12

                          Yes. I apologise - I was in grumpy-old-git mode when I wrote my comment. It only happens because I am a grumpy old git, but some days I'm better at hiding the fact. :(

                          Phil


                          The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.

                          1 Reply Last reply
                          0
                          • S Stephen Hewitt

                            The reason is simple. The it++ in the for loop should not be present if the it = m_WorkerThreadList.erase(it); line is executed as if you do both you've skipped two elements not one!

                            Steve

                            D Offline
                            D Offline
                            DMartens
                            wrote on last edited by
                            #13

                            And also ++it as you are using C++ for speed. Ditlef

                            1 Reply Last reply
                            0
                            • M Mr Brainley

                              I wrote this :

                              while (!(m\_WorkerThreadList.empty()))
                              {
                              	for (	it = m\_WorkerThreadList.begin(); 
                              			it != m\_WorkerThreadList.end();
                              			it++)
                              		if (!((\*it)->isAlive()))
                              		{
                              			delete (\*it);
                              			it	= m\_WorkerThreadList.erase(it);
                              		}
                              }
                              

                              I found, that not all elements are actually deleted. The programm did not crash though, unless i used the debugger. If i just let the Debug build run, things where fine, except for a memory leak. If i used a debugger, the programm chrashed due to memory violation. However, that's not the problem here. It's a classic for loop thing.

                              R Offline
                              R Offline
                              RexFury
                              wrote on last edited by
                              #14

                              You tried : if (!((*it)->isAlive())){ delete (*it); it = m_WorkerThreadList.erase(it); } Why not it.isAlive() & delete(it) ????? Just a newbie question.... Rex

                              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