Re: Does push_back() do a copy?
On 28 Jun, 17:20, Angus <anguscom...@gmail.com> wrote:
On 28 Jun, 16:11, Erik Wikstr=F6m <Erik-wikst...@telia.com> wrote:
The correct solution is to do like you did, use new to create the
sockets,
within an appropriate RAII object (e.g. a smart pointer),
objects that own the
pointers to the collection, and *don't* call delete.
Then, later when you are done with the sockets
.... the memory management is already fully solved and has become a non-
issue.
So to delete I do this:
for (std::vector<CTestClientSocket*>::iterator it =
m_collClients.begin();
it != m_collClients.end(); it++)
{
delete *it;}
m_collClients.erase(m_collClients.begin(), m_collClients.end());
That's what Erik suggested. However, it's not a very good solution.
Any time you have to write new or delete anywhere except in a class
designed to manage memory should ring big alarm bells.
Does that look right to you?
No. You're not using RAII. Someone has already suggested using a smart
pointer (boost::shared_ptr was the example). Do that - give the
responsibility for memory management to an object designed for the
purpose. You have much more important things to worry about.
Gavin Deane