Re: Find error
On Nov 30, 11:09 am, Joel Yliluoma <bisq...@iki.fi> wrote:
On Thu, 29 Nov 2007 23:42:52 -0800 (PST), yayalee1...@gmail.com wrote:
is there any error in the following code?
Apart from the obvious problems preventing successful
compilation, I'll point one less obvious one:
void changebackgroud(std::istream& new)
{
lock(&fmutex);
delete fimage;
++changnum;
fimage=new image(new);
unlock(&fmutex);
}
If 'new' throws an exception here, fmutex remains
locked (unlock is not on the return path) and nobody
can lock it again.
If 'new' throws an exception here, he can no longer access the
object, not even to destruct it. Loosing the lock is part of
the problem, but leaving an invalid pointer in fimage is also a
fatal error.
In short, he must do everything that can fail before the delete
(which leaves the pointer invalid). And the lock isn't
necessary except around the parts which can't fail, either.
(It's probably not desirable for the creation of the new image
to be protected by the lock either, since it's likely to be a
very long process.)
--
James Kanze (GABI Software) email:james.kanze@gmail.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