STL vector.push_back causes delete "object"

Discussion in 'C++' started by Hitesh Bhatiya, Aug 13, 2003.

  1. 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<SocketClient,std::allocator<SocketClient>
    >::__destroy(this = 0x2209c4, start = 0x31ac48, finish = 0x31ac68), line 147

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

    in "vector.cc"
    [6] std::vector<SocketClient,std::allocator<SocketClient>
    >::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, Aug 13, 2003
    #1
    1. Advertising

  2. 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 <>, 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<SocketClient>,SocketClient>::destroy
    > (this = 0xfe909b2f, p = 0x31ac38), line 520 in "memory"
    > [4] std::vector<SocketClient,std::allocator<SocketClient>
    >>::__destroy(this = 0x2209c4, start = 0x31ac48, finish = 0x31ac68), line 147

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

    > in "vector.cc"
    > [6] std::vector<SocketClient,std::allocator<SocketClient>
    >>::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.
    >
    >
    >
    Ronald Landheer-Cieslak, Aug 13, 2003
    #2
    1. Advertising

  3. Hitesh Bhatiya

    Howard Guest

    "Hitesh Bhatiya" <> wrote in message
    news:...
    > 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
    Howard, Aug 13, 2003
    #3
  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
    Patrick Frankenberger, Aug 13, 2003
    #4
  5. "Hitesh Bhatiya" <> wrote in message
    news:...
    > 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
    John Harrison, Aug 13, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    1,914
    Csaba
    Feb 18, 2006
  2. Replies:
    11
    Views:
    1,160
    Kai-Uwe Bux
    Apr 4, 2006
  3. toton
    Replies:
    9
    Views:
    1,813
    toton
    Aug 11, 2006
  4. toton
    Replies:
    0
    Views:
    373
    toton
    Aug 10, 2006
  5. zl2k
    Replies:
    27
    Views:
    1,576
    Francesco S. Carta
    Sep 7, 2010
Loading...

Share This Page