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. 4 9771
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.
"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
"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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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)
{
|
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...
|
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 =...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |