473,545 Members | 2,715 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL vector.push_bac k 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(_s ock);
connections.pus h_back(*newSock );
[end code fragement]

[stack trace]

=>[1] SocketClient::~ SocketClient(th is = 0x31ac38), line 18 in
"SocketClient.C "
[2] __rwstd::__dest roy<SocketClien t>(pointer = 0x31ac38), line 184 in
"memory"
[3]
std::allocator_ interface<std:: allocator<Socke tClient>,Socket Client>::destro y
(this = 0xfe909b2f, p = 0x31ac38), line 520 in "memory"
[4] std::vector<Soc ketClient,std:: allocator<Socke tClient>
::__destroy(th is = 0x2209c4, start = 0x31ac48, finish = 0x31ac68), line 147 in "vector"
[5] std::vector<Soc ketClient,std:: allocator<Socke tClient>::__insert_aux (this = 0x2209c4, position = 0x31ac68, x = CLASS), line 141 in "vector.cc"
[6] std::vector<Soc ketClient,std:: allocator<Socke tClient>::push_back(th is = 0x2209c4, x = CLASS), line 467 in "vector"

[7] ConnectionHandl er::run(this = 0xffbefa30), line 73 in
"ConnectionHand ler.C"
[8] threadEntryPoin t(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 9778
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.c lara.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(_s ock);
connections.pus h_back(*newSock );
[end code fragement]

[stack trace]

=>[1] SocketClient::~ SocketClient(th is = 0x31ac38), line 18 in
"SocketClient.C "
[2] __rwstd::__dest roy<SocketClien t>(pointer = 0x31ac38), line 184 in
"memory"
[3]
std::allocator_ interface<std:: allocator<Socke tClient>,Socket Client>::destro y
(this = 0xfe909b2f, p = 0x31ac38), line 520 in "memory"
[4] std::vector<Soc ketClient,std:: allocator<Socke tClient>
::__destroy(t his = 0x2209c4, start = 0x31ac48, finish = 0x31ac68), line 147

in "vector"
[5] std::vector<Soc ketClient,std:: allocator<Socke tClient>
::__insert_au x(this = 0x2209c4, position = 0x31ac68, x = CLASS), line 141

in "vector.cc"
[6] std::vector<Soc ketClient,std:: allocator<Socke tClient>
::push_back(t his = 0x2209c4, x = CLASS), line 467 in "vector"

[7] ConnectionHandl er::run(this = 0xffbefa30), line 73 in
"ConnectionHand ler.C"
[8] threadEntryPoin t(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(_s ock);
connections.pus h_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(_s ock);
connections.pus h_back(*newSock );
[end code fragement]


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

SocketClient newSock(_sock);
connections.pus h_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<SocketCl ient> connections;

then SocketClient must have valid copy constructor and assignment operator.

The less good alternative is to use pointers

vector<SocketCl ient*> 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(_s ock);
connections.pus h_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<Soc ketClient*> connections;
and use
connections.pus h_back(new SocketClient(_s ock);
..

HTH,
Patrick
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
6714
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 with "first-class airplane ticket". I just find the name a bit strange. Also, if there are first-class objects, what would the second-class objects or...
22
3301
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
1281
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 reference not set to an instance of an object” this is what the code looks like Dim lador_JLD As New ADODB.Recordset ls_sql = "execute...
4
16276
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 (Form_subfrmTest) and some code was inserted to it. So I deleted the code, but the entry in the VBA editor remained (Form_subfrmTest). I didn't find any mean to...
5
3987
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 grid like this: private void Page_Load(object sender, System.EventArgs e) { BlogEntry entries = DataLayer.GetAllBlogEntries();...
2
3605
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 disposed object". I understand that it is because as soon as the form is closed it will in turn try to dispose the grid. I have unwired the event in...
5
3326
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
17135
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 object. I debug using gdb to find the error and located the position where it is throwing an error
14
2500
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 = MyClass() a.b = 1 # OK
0
7496
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7685
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7941
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7452
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5354
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3485
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1039
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.