On Wed, Aug 15, 2007 at 12:27:18AM -0600, Warren Young wrote:
> Smart pointers are tricky, and automatic memory management is even
> trickier, and I just created a template that does both. I swapped it in
> for existing code in MySQL++ that used value semantics and it passes the
> test suite, but I'd still appreciate someone else taking a look at it.
>
> http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/refcounted.h?view=markup
Just some nit-pick comments. :-)
void assign(T* c)
{
detach();
if (c) {
counted_ = c;
refs_ = new size_t(1);
}
else {
counted_ = 0;
refs_ = 0;
}
}
In a very rare case, the call to:
assign(new SomeObject);
will leak memory if the new size_t(1) runs out of memory and throws an
exception.
Also, I'm more paranoid, and would probably zero the pointers in
detach() anyway, just in case of that new exception.
Although if new throws, we have bigger problems to worry about. :-)
I usually don't even bother catching out of memory exceptions. But
a library probably shouldn't leak, in case there are people who do
catch out-of-mem.
- Chris