By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,191 Members | 820 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,191 IT Pros & Developers. It's quick & easy.

STL vector.push_back causes delete "object"

P: n/a
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<SocketClie nt>,SocketClient>::destroy
(this = 0xfe909b2f, p = 0x31ac38), line 520 in "memory"
[4] std::vector<SocketClient,std::allocator<SocketClie nt>
::__destroy(this = 0x2209c4, start = 0x31ac48, finish = 0x31ac68), line 147 in "vector"
[5] std::vector<SocketClient,std::allocator<SocketClie nt>::__insert_aux(this = 0x2209c4, position = 0x31ac68, x = CLASS), line 141 in "vector.cc"
[6] std::vector<SocketClient,std::allocator<SocketClie nt>::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.

Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

In article <10****************@damia.uk.clara.net>, Hitesh Bhatiya wrote:
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<SocketClie nt>,SocketClient>::destroy
(this = 0xfe909b2f, p = 0x31ac38), line 520 in "memory"
[4] std::vector<SocketClient,std::allocator<SocketClie nt>
::__destroy(this = 0x2209c4, start = 0x31ac48, finish = 0x31ac68), line 147

in "vector"
[5] std::vector<SocketClient,std::allocator<SocketClie nt>
::__insert_aux(this = 0x2209c4, position = 0x31ac68, x = CLASS), line 141

in "vector.cc"
[6] std::vector<SocketClient,std::allocator<SocketClie nt>
::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.

Jul 19 '05 #2

P: n/a

"Hitesh Bhatiya" <no****@hotmail.com> wrote in message
news:10****************@damia.uk.clara.net...
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
Jul 19 '05 #3

P: n/a

"Hitesh Bhatiya" <no****@hotmail.com> wrote in message
news:10****************@damia.uk.clara.net...
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
Jul 19 '05 #4

P: n/a

"Hitesh Bhatiya" wrote:
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
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.