473,770 Members | 5,284 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

for_each iteration in a std::list adds a new element and nextiteration crashes

Hi all,

I have this code:

class A
{
std::list<Bm_ob servadores;

void function()
{

ApplySendMessag e apply(this, message);
std::for_each(m _observadores.b egin(),
m_observadores. end(),
apply);
}
}

and, ApplySendMessag e::operator() adds a new item to the list
m_observadores under some conditions.

I get (MSVC8.0) "list iterator not incrementable" in an iteration of
for_each algorithm after adding (m_observadores .push_back) a new item
to the list in the previous iteration (ApplySendMessa ge =
AplicarEnviarMe nsaje):

msvcp80d.dll!st d::_Debug_messa ge(const wchar_t * message=0x005fb f30,
const wchar_t * file=0x005fbb50 , unsigned int line=236) Línea 24 C++
> libcliente.dll! std::list<maevi f::cliente::Obs ervadorGestorEn tidades *,std::allocato r<maevif::clien te::ObservadorG estorEntidades *::_Const_itera tor<0>::operato r++() Línea 236 + 0x17 bytes C++
libcliente.dll! std::list<maevi f::cliente::Obs ervadorGestorEn tidades
*,std::allocato r<maevif::clien te::ObservadorG estorEntidades *>
>::_Iterator<0> ::operator++() Línea 399 C++
libcliente.dll!
std::for_each<s td::list<maevif ::cliente::Obse rvadorGestorEnt idades
*,std::allocato r<maevif::clien te::ObservadorG estorEntidades *>
>::_Iterator<1> ,maevif::client e::AplicarEnvia rMensaje>(std:: list<maevif::cl iente::Observad orGestorEntidad es
*,std::allocato r<maevif::clien te::ObservadorG estorEntidades *>
>::_Iterator<1_ First=0x0249b93 c,
std::list<maevi f::cliente::Obs ervadorGestorEn tidades
*,std::allocato r<maevif::clien te::ObservadorG estorEntidades *>
>::_Iterator<1_ Last=0xcdcdcdcd ,
maevif::cliente ::AplicarEnviar Mensaje _Func={...}) Línea 28 + 0x8
bytes C++
Is the iterator invalidated after the insertion or my error comes from
another place? Is this standard?

m_observadores. push_front instead of m_observadores. push_back will fix
that?

Thank you.
Jun 27 '08 #1
0 1730

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3959
by: Mike Pemberton | last post by:
I'm sure there's a good explanation for this effect, but I get rather a strange output from this little test: #include <iostream> #include <list> int main() { std::list<int> int_list;
3
6811
by: Kyle Teague | last post by:
I have a list of pointers to structs as a private member of a class. If I call begin() in the same function as I added the data then no access violation occurs. However, if I try to call begin() in a member function of the same class I get a memory access violation. For example: // this is fine, no error void CBase::FuncA( void ) { plugin_info_t *plugin_info = new plugin_info_t;
6
6662
by: PengYu.UT | last post by:
Hi, Suppose I have a list which contains pointers. I want the pointer got by dereferencing the iterator be a pointer pointing to a const object. But std::list<const T*>::const_iterator doens't give me this capability. So I want std::list<T*>::iterator. However, the container is of type std::list<T*>. How to get std::list<const T*>::iterator?
25
3886
by: Markus Svilans | last post by:
Hi, There seems to be some functionality missing from the STL. I am iterating through a linked list (std::list) using a reverse iterator and attempting to erase certain items from the list. It is important that I iterate through the list backwards, because the items in it have to be processed in reverse order before erasing. However, there does not appear to be an std::list::erase() method defined for reverse iterators.
4
6550
by: Tim Slattery | last post by:
It would be convenient for my app to store the stuff I'm generating in a std::list. I'd like to remember the location of a particular place in the list - sort of like sticking my finger into it - and insert an entry in that place some time later. There's an "insert" member function, but it takes an iterator to designate the place to insert. And I can't figure out how to get an iterator that points to the right place. For instance: ...
3
3730
by: Andy | last post by:
Hello, I have the following situation: Thread A is allocating a dataset, doing some low-level calculations and storing a pointer to the dataset in a std::list via push_back. Thread B should retrieve the pointer to the first dataset in the list, remove it from the list, and do some high level analysis. The problem now is, how do I efficiently retrieve the pointer?
15
4159
by: desktop | last post by:
If I have a sorted std::list with 1.000.000 elements it takes 1.000.000 operations to find element with value = 1.000.000 (need to iterator through the whole list). In comparison, if I have a std::set with 1.000.000 element it will only take approx lg 1.000.000 = 20 operations! Can it really be true that the difference is a factor of 1.000.000/20 = 50.000 in this case?
4
3899
by: TBass | last post by:
Hi, I've got a class that uses a std::list to store tags that need to be written out to a device. I've had no problem with the lists in terms of iterating and reading values. My problem is when I need to pop that value off the list when I'm done with it. In this case, I think the iterator is getting "lost" because I pop off the value it is pointing to at the end of this function. But how do I pop values off one at a time without the...
11
4169
by: Juha Nieminen | last post by:
Assume we have this: std::list<Typelist1(10, 1), list2(20, 2); std::list<Type>::iterator iter = list1.end(); list1.swap(list2); What happens here, according to the standard? 1) 'iter' still points to list1::end(). 2) 'iter' now points to list2::end().
0
9591
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10225
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10001
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9867
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7415
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.