|
From: Shane B. <ast...@te...> - 2003-07-09 23:00:54
|
MessageYes, I appologize for not including the header definition. Here =
it is.
#ifndef USERLIST_H
#define USERLIST_H
=20
#include <map.h>
=20
class User;
=20
class UserList {
public:
struct ltstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) < 0;
}
};
typedef std::map<const char *, User *, ltstr> UserMap;
=20
UserList();
~UserList();
=20
void AddUser(User *pUser);
bool DeleteUser(const char *pNick);
User *FindUser(const char *pNick);
unsigned long Length();
=20
UserMap m_mapUsers;
};
=20
=20
#endif
-Shane
----- Original Message -----=20
From: Ioannis Vranos=20
To: 'C++'=20
Sent: Wednesday, July 09, 2003 4:41 PM
Subject: RE: [Dev-C++] STL Map question
-----Original Message-----
From: dev...@li... =
[mailto:dev...@li...] On Behalf Of Shane =
Bryldt
Sent: Thursday, July 10, 2003 12:54 AM
To: C++
Subject: [Dev-C++] STL Map question
Hello,
I recently ran into an issue that I can't seem to pinpoint the =
cause of. 99% of the time, it seems to work, so it seems to be a small =
situational bug. Subject, is a UserList class, which internally =
implements an STL map. I am not sure why, but it seems to crash =
occasionally when I attempt to call the map::find function, with a valid =
null terminated char array. The map template implements a simple case =
insensative comparison. As I said, it works more often than not, but =
for some reason when attempting to delete from the list sometimes, it =
causes a SEGFAULT. Here is what GDB has to say:
Program received signal SIGSEGV, Segmentation fault.
0x80bb142 in _Rb_tree<char const *, pair<char const *const, User *>, =
_Select1st<pair<char const *const, User *> >, UserList::ltstr, =
allocator<User *> >::find (
this=3D0xbf14b30, __k=3D@0xbfbff7bc) at =
/usr/include/g++/stl_tree.h:1118
1118 _Link_type __x =3D _M_root(); // Current node.=20
(gdb) up
#1 0x80bb529 in map<char const *, User *, UserList::ltstr, =
allocator<User *> >::find (this=3D0xbf14b30, __x=3D@0xbfbff7bc) at =
/usr/include/g++/stl_map.h:187
187 iterator find(const key_type& __x) { return =
_M_t.find(__x); }
(gdb) up
#2 0x80ba8e9 in UserList::DeleteUser (this=3D0xbf14b30,=20
pNick=3D0xbf12300 "_=C7=E1=DF=D1=D2_") at UserList.cpp:26
26 UserMap::iterator it =3D m_mapUsers.find(pNick);
(gdb) print pNick
$1 =3D 0xbf12300 "_=C7=E1=DF=D1=D2_"
And here is the code in question.
bool UserList::DeleteUser(const char *pNick)
{
bool bRet =3D false;
=20
if(m_mapUsers.size() > 0)
{
UserMap::iterator it =3D m_mapUsers.find(pNick);
if(it !=3D m_mapUsers.end())
{
m_mapUsers.erase(it);
bRet =3D true;
}
}
return bRet;
}
My first thought was that the MAP container had issues when =
attempting to "find" with 0 elements, but that didn't seem to affect =
anything. Any thoughts?=20
Are you sure that m_mapUsers is an object of type UserMap and UserMap =
is a typedef of a map type?
Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net=20
|