Re: Is this the wrong way to use std::list?
On Jan 4, 2:26 pm, TBass <t...@automateddesign.com> wrote:
So I have a class:
unsigned int ClientID;
Lets assume that the compiler generated copy ctor for Client works for
AddClient( Client *pClient);
It would be interesting to know how you are managing the pointer
void AddClient( const Client& client ); // much better
RemoveClient( unsigned int ID );
To add clients to the list, AddClient is called:
MyListenSocket::AddClient( Client *pClient )
m_listClients.push_back( *pClient );
But a client can be erased from anywhere on the list. I wrote this
MyListenSocket::RemoveClient( unsigned int ID )
void MyListenSocket::RemoveClient( const unsigned id )
typedef std::list<Client>::iterator LIter;
for( it = m_listClients.begin();
it != m_listClients.end();
if ( it->ClientID() == ID )
m_listClients.erase( it );
The problem is that this seems to corrupt the heap in my program. I
know that the iterator is corrupted when I use the erase command, but
why would that corrupt the heap?
The iterator is not corrupted, it gets invalidated. Your free store is
suffering from something else.
i'ld probably use a std::set instead of a std::list.
Is this not the way to delete an item from the middle of a list?
Should I not be using ::list for this type of purpose?
Thanks in advance,