Re: Does push_back() do a copy?
On 6/28/2007 12:52 PM, Angus wrote:
I have a socket class CTestClientSocket which I am using to simulate
load testing.
I create multiple instances of the client like this:
for (int i = 0; i < 5; i++)
{
CTestClientSocket* pTemp = new CTestClientSocket(this, ip, port);
pTemp->Connect();
m_collClients.push_back(pTemp);
// delete pTemp;
}
Note I have commented out the delete pTemp
Then I send some data to a server like this:
// iterate through collection of clients
for (std::vector<CTestClientSocket*>::iterator it =
m_collClients.begin();
it != m_collClients.end(); it++)
{
(*it)->Send(byData);
}
This works if delete pTemp on the Connect is commented out. But I
thought that push_back would make a copy of pTemp.
And what happens if you copy a pointer ?
and so I could
then delete pTemp. But if I delete pTemp I get an memory access
problem in the (*it)->Send(byData);
Because you have a dangling pointer.
And I have to cleanup at the end. Does push_back NOT do a copy? I am
confused, can someone explain.
Or is there a better way to do this sort of thing?
Use boost::shared_ptr<> for example.
S.
--
Stefan Naewe stefan dot naewe at atlas-elektronik dot com
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html