From: Werner S. <ws...@se...> - 2007-12-15 09:02:27
|
On Samstag 15 Dezember 2007, Tim wrote: ... > ***----------------------------------------------------------*** > While we are on the subject of programming style, I would like to alert > the programmers out there to watch out for a particular > Standard Template Library no-no: I call it the 'delete while iterating' bug: > In Muse I had to fix several places where someone iterated an STL 'map' > or 'list', and would conditionally delete items from the map or list, while > still iterating the map or list. This is a no-no! The iterator becomes > invalid after deleting an item, so the whole iteration has to be started > over again. > *** Take a look in Muse 0.9, in ./muse/seqmsg.cpp: around line 525: > In the function Audio::msgRemoveTracks() > Read the comments I placed in there. Learn it, use it ! > > Comments welcome! Cheers. Tim. thats the reason i recently more use the Qt containers. Besides their nicer syntax they use (as most Qt objects) "copy on write" which also works with threads. For example in a "foreach" loop you iterate over a container copy. This allows deleting of items in the loop without problems. I found lot of situations were Qt containers are much easier to use than STL. Werner |