473,246 Members | 1,490 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

STL vector.push_back causes delete "object"

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
4 9762
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

"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

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Hung Jung Lu | last post by:
Hi, Does anybody know where this term comes from? "First-class object" means "something passable as an argument in a function call", but I fail to see the connection with "object class" or...
22
by: Dr Duck | last post by:
GDay all, Something seems odd to me.... I wrote a simple C# function public void bind(ref object a, ref object b, bool atob) { if(atob) b = a; else
0
by: howie | last post by:
I've upgraded a vb6 application to vb .net and am having an issue. whenever I try to set the recordset property of the VB6.adodc object in .net and run the application I get the error “object...
4
by: Georges Heinesch | last post by:
Hi. This question might seem trivial, but I didn't find any solution. By error, I created some event for a subform (subfrmTest). Hence, an entry in the VBA editor list was made...
5
by: Christian Hvid | last post by:
What is the easiest way to get the "row object" or "item object" when a datagrid is clicked? I have web form with a datagrid. And I have an array of something called BlogEntry that I bind to the...
2
by: Rajat Tandon | last post by:
Hi, I have a grid which is continuously updating by the data from a external event. When I close the form on which the grid is placed, then it gives the error message ... "Can not access a...
5
by: Frederick Gotham | last post by:
If we have a simple class such as follows: #include <string> struct MyStruct { std::string member; MyStruct(unsigned const i) {
3
by: klaritydefect | last post by:
Hi, I am receving the following error when I run my application (build on C ++ code) Program received signal SIGSEGV, Segmentation fault si_code: 1 - SEGV_MAPERR - Address not mapped to...
14
by: =?GB2312?B?zPC5zw==?= | last post by:
Howdy, I wonder why below does not work. a = object() a.b = 1 # dynamic bind attribute failed... To make it correct, we have to create a new class: class MyClass(object): pass a =...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.