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:
class Client
{
unsigned int ClientID;
....
};
Lets assume that the compiler generated copy ctor for Client works for
now.
class MyListenSocket
{
...
AddClient( Client *pClient);
It would be interesting to know how you are managing the pointer
above.
public:
void AddClient( const Client& client ); // much better
RemoveClient( unsigned int ID );
private:
std::list<Client> m_listClients;
};
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
function:
MyListenSocket::RemoveClient( unsigned int ID )
void MyListenSocket::RemoveClient( const unsigned id )
{
std::list<Client>::iterator it;
typedef std::list<Client>::iterator LIter;
LIter it;
for( it = m_listClients.begin();
it != m_listClients.end();
++it )
{
if ( it->ClientID() == ID )
{
m_listClients.erase( it );
break;
}
}
}
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,
Tom Junior