std::list problems

S

sidaf

Hello,

I'm writing a connection management class, but I'm running into
problems with the std::list template. I want to use a list to help
manage my Connection objects.

The ConnectionManager header file has the following inside the class
definition:

private:
typedef std::list<Connection *> ConnectionList;
typedef std::list<Connection *>::const_iterator const_iterator;
ConnectionList connections;

I've got the following method inside my ConnectionManager class.

void ConnectionManager::add(Connection *c)
{
connections.push_back(c);
}

When I call that method using the following snippet of code:

cm->add(new Connection(fd));

I get a segmentation fault. I've ran gdb on the core file and a
backtrace looks like this:

(gdb) backtrace
#0 0xb7f0a52c in std::_List_node_base::hook () from
/usr/lib/libstdc++.so.6
#1 0x0804def9 in std::list said:
::_M_insert (this=0xb7f8d008, __position={_M_node = 0xb7f8d008}, __x=@0xbfcb9374)
at stl_list.h:1153
#2 0x0804df36 in std::list said:
::push_back (this=0xb7f8d008, __x=@0xbfcb9374) at stl_list.h:774
#3 0x0804db39 in ConnectionManager::add (this=0xb7f8d004, c=0x806f098)
at connectionmanager.cpp:19
#4 0x0804c6ec in SocketManager::processReady (this=0xbfcb957c,
s=@0xbfcb93f0)
at socketmanager.cpp:52
#5 0x08049866 in main (argc=1, argv=0xbfcb9634) at enraged.cpp:37
(gdb)

I'm rather new to C++ (and gdb), so I'm a bit confused as to why this
is failing.

Can anybody guide me in the right direction?

Regards,

Sidaf
 
F

Frank Puck

sidaf said:
Hello,

I'm writing a connection management class, but I'm running into
problems with the std::list template. I want to use a list to help
manage my Connection objects.

The ConnectionManager header file has the following inside the class
definition:

private:
typedef std::list<Connection *> ConnectionList;
typedef std::list<Connection *>::const_iterator const_iterator;
ConnectionList connections;

I've got the following method inside my ConnectionManager class.

void ConnectionManager::add(Connection *c)
{
connections.push_back(c);
}

When I call that method using the following snippet of code:

cm->add(new Connection(fd));

I get a segmentation fault. I've ran gdb on the core file and a
backtrace looks like this:

(gdb) backtrace
#0 0xb7f0a52c in std::_List_node_base::hook () from
/usr/lib/libstdc++.so.6

at stl_list.h:1153

#3 0x0804db39 in ConnectionManager::add (this=0xb7f8d004, c=0x806f098)
at connectionmanager.cpp:19
#4 0x0804c6ec in SocketManager::processReady (this=0xbfcb957c,
s=@0xbfcb93f0)
at socketmanager.cpp:52
#5 0x08049866 in main (argc=1, argv=0xbfcb9634) at enraged.cpp:37
(gdb)

I'm rather new to C++ (and gdb), so I'm a bit confused as to why this
is failing.

Can anybody guide me in the right direction?

Regards,

Sidaf



this code looks totally correct.
Something must be wrong with the state of your program before this has been
executed.
Use valdgrind to find such kind of bugs on INTEL LINUX.
Use purify on other systems
 
V

Victor Bazarov

sidaf said:
I'm writing a connection management class, but I'm running into
problems with the std::list template. I want to use a list to help
manage my Connection objects.

The ConnectionManager header file has the following inside the class
definition:

private:
typedef std::list<Connection *> ConnectionList;
typedef std::list<Connection *>::const_iterator const_iterator;
ConnectionList connections;

Seems fine to me.
I've got the following method inside my ConnectionManager class.

void ConnectionManager::add(Connection *c)
{
connections.push_back(c);
}

No problem here AFAICS.
When I call that method using the following snippet of code:

cm->add(new Connection(fd));

What's "cm"? How is it allocated?
I get a segmentation fault.

What's "Connection" how does it get created with an argument?
I've ran gdb on the core file and a
backtrace looks like this:

[...]

That's irrelevant in comp.lang.c++. C++ does not define 'backtrace'.
I'm rather new to C++ (and gdb),

'gdb' is not part of C++. If you need help with 'gdb', you need to
find a better place than c.l.c++. Try 'gnu.utils.help' or try some
online (web) GNU forums.
so I'm a bit confused as to why this
is failing.

Can anybody guide me in the right direction?

This is convered in FAQ 5.8. Please read the FAQ before posting.
You can find FAQ Lite here: http://www.parashift.com/c++-faq-lite/

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top