Re: Does push_back() do a copy?
On Jun 28, 8:37 pm, Gavin Deane <deane_ga...@hotmail.com> wrote:
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
within an appropriate RAII object (e.g. a smart pointer),
It's hard to imagine one with the appropriate semantics for a
socket. This is exactly the sort of thing where one wants
add the 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-
Memory management is best solved by using the Boehm collector.
The problem here isn't just memory management, it's lifetime
management. When does the connection exist? And it's something
that needs to be explicitly handled.
So to delete I do this:
for (std::vector<CTestClientSocket*>::iterator it =
it != m_collClients.end(); it++)
That's what Erik suggested. However, it's not a very good solution.
It depends. I can see cases where it would be appropriate, e.g.
program shutdown. Most of the time, however, I imagine that the
socket will be deleted and removed from the container
explicitly, in reaction to some external event.
Any time you have to write new or delete anywhere except in a class
designed to manage memory should ring big alarm bells.
Anytime you start thinking that smart pointers are going to
solve all your object lifetime problems, a big alarm bell should
ring. Memory management is the role of the garbage collector
(you do use the Boehm collector, don't you). Object lifetime is
a design issue, and until you have defined what you want, you
can't decide the correct solution to manage it.
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).
Which for something like Socket is almost certainly a poor
choice, which will likely end up with a lot of dead objects
Do that - give the
responsibility for memory management to an object designed for the
purpose. You have much more important things to worry about.
For memory management, certainly. But shared_ptr doesn't really
solve that very well; the Boehm collector is the simplest answer
for that. As for object lifetime management, there's no way you
can avoid it; it's an important design issue.
James Kanze (GABI Software) email:email@example.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34