STL vector.push_back causes delete "object"

H

Hitesh Bhatiya

Hi all,

I have written a small program to accept some socket connections, which are
then added to a vector (using push_back). But after a few calls to the
push_back function, it deleted the object that was added last.

Could someone please tell me why this happens ? Am I doing something wrong
here ?

[code fragment]
SocketClient* newSock=new SocketClient(_sock);
connections.push_back(*newSock);
[end code fragement]

[stack trace]

=>[1] SocketClient::~SocketClient(this = 0x31ac38), line 18 in
"SocketClient.C"
[2] __rwstd::__destroy<SocketClient>(pointer = 0x31ac38), line 184 in
"memory"
[3]
std::allocator_interface<std::allocator<SocketClient>,SocketClient>::destroy
(this = 0xfe909b2f, p = 0x31ac38), line 520 in "memory"
[4] std::vector said:
::__destroy(this = 0x2209c4, start = 0x31ac48, finish = 0x31ac68), line 147
in "vector"
[5] std::vector said:
::__insert_aux(this = 0x2209c4, position = 0x31ac68, x = CLASS), line 141
in "vector.cc"
[6] std::vector said:
::push_back(this = 0x2209c4, x = CLASS), line 467 in "vector"
[7] ConnectionHandler::run(this = 0xffbefa30), line 73 in
"ConnectionHandler.C"
[8] threadEntryPoint(thread = 0xffbefa30), line 10 in "Thread.C"

[end stack trace]

Other info:

$ uname -X
System = SunOS
Node = bb18
Release = 5.8
KernelID = Generic_108528-20
Machine = sun4u
BusType = <unknown>
Serial = <unknown>
Users = <unknown>
OEM# = 0
Origin# = 1
NumCPU = 1

$ CC -V
CC: Sun WorkShop 6 update 2 C++ 5.3 2001/05/15



Thanks.
 
R

Ronald Landheer-Cieslak

push_back copies the value into your vector. Hence, it calls the copy-
constructor for the SocketClient class, followed by the destructor of
SocketClient on the old (copied) object. Make sure you have a working copy-
constructor to not loose your object's value :)

In your case, I'd make it a reference-counting class and not destroy the
value it holds until the last reference is destroyed, but that's assuming
your class is just there to hold the info on a socket..

HTH

rlc

Hi all,

I have written a small program to accept some socket connections, which are
then added to a vector (using push_back). But after a few calls to the
push_back function, it deleted the object that was added last.

Could someone please tell me why this happens ? Am I doing something wrong
here ?

[code fragment]
SocketClient* newSock=new SocketClient(_sock);
connections.push_back(*newSock);
[end code fragement]

[stack trace]

=>[1] SocketClient::~SocketClient(this = 0x31ac38), line 18 in
"SocketClient.C"
[2] __rwstd::__destroy<SocketClient>(pointer = 0x31ac38), line 184 in
"memory"
[3]
std::allocator_interface<std::allocator<SocketClient>,SocketClient>::destroy
(this = 0xfe909b2f, p = 0x31ac38), line 520 in "memory"
[4] std::vector said:
::__destroy(this = 0x2209c4, start = 0x31ac48, finish = 0x31ac68), line 147
in "vector"
[5] std::vector said:
::__insert_aux(this = 0x2209c4, position = 0x31ac68, x = CLASS), line 141
in "vector.cc"
[6] std::vector said:
::push_back(this = 0x2209c4, x = CLASS), line 467 in "vector"
[7] ConnectionHandler::run(this = 0xffbefa30), line 73 in
"ConnectionHandler.C"
[8] threadEntryPoint(thread = 0xffbefa30), line 10 in "Thread.C"

[end stack trace]

Other info:

$ uname -X
System = SunOS
Node = bb18
Release = 5.8
KernelID = Generic_108528-20
Machine = sun4u
BusType = <unknown>
Serial = <unknown>
Users = <unknown>
OEM# = 0
Origin# = 1
NumCPU = 1

$ CC -V
CC: Sun WorkShop 6 update 2 C++ 5.3 2001/05/15



Thanks.
 
H

Howard

Hitesh Bhatiya said:
Hi all,

I have written a small program to accept some socket connections, which are
then added to a vector (using push_back). But after a few calls to the
push_back function, it deleted the object that was added last.

Could someone please tell me why this happens ? Am I doing something wrong
here ?

[code fragment]
SocketClient* newSock=new SocketClient(_sock);
connections.push_back(*newSock);
[end code fragement]

I don't know if this will help or not, but how about if you push_back copies
of the pointers themselves, instead of dereferencing them like that?
Perhaps there's a problem with your copy-constructor of something for that
object, and you're getting an exception thrown in the constructor? Using
the pointers instead would prevent that extra copy step.

-Howard
 
P

Patrick Frankenberger

Hitesh Bhatiya said:
Hi all,

I have written a small program to accept some socket connections, which are
then added to a vector (using push_back). But after a few calls to the
push_back function, it deleted the object that was added last.

Could someone please tell me why this happens ? Am I doing something wrong
here ?

[code fragment]
SocketClient* newSock=new SocketClient(_sock);
connections.push_back(*newSock);
[end code fragement]

In this fragment you create a new SocketClient, then you create a copy of it
and append this copy to the end of the vector. If the vector needs to resize
itself it copys all elements to the new location and destructs the ones at
the old location.

Propably you want connections to store the object you created with new. So
change connections to be
std::vector<SocketClient*> connections;
and use
connections.push_back(new SocketClient(_sock);
..

HTH,
Patrick
 
J

John Harrison

Hitesh Bhatiya said:
Hi all,

I have written a small program to accept some socket connections, which are
then added to a vector (using push_back). But after a few calls to the
push_back function, it deleted the object that was added last.

Could someone please tell me why this happens ? Am I doing something wrong
here ?

[code fragment]
SocketClient* newSock=new SocketClient(_sock);
connections.push_back(*newSock);
[end code fragement]

One thing wrong is that you are pointlessly allocating with new, try this

SocketClient newSock(_sock);
connections.push_back(newSock);

The second thing wrong (almost certainly) is that you haven't defined valid
copy constructor and assignment operators for your SocketClient class.

Perhaps this second wrong thing was why you tried the first wrong thing. But
there is no getting round it, if you write

vector<SocketClient> connections;

then SocketClient must have valid copy constructor and assignment operator.

The less good alternative is to use pointers

vector<SocketClient*> connections;

john
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top