[Insight-users] Re: Insight-users Digest, Vol 43, Issue 7
Gaëtan Lehmann
gaetan.lehmann at jouy.inra.fr
Thu Nov 8 08:37:07 EST 2007
Hi Julia,
Le 5 nov. 07 à 21:07, Julia Smith a écrit :
> 3.4.0
> I haven't built it yet, but did take a look at itkLightObject. Now
> UnRegister does correct multi-threading thing of _not_ referencing
> a volatile value outside a semaphore, SetRegister() should do the
> same thing. It's just not thread safe to check values modifiable by
> another context. A race condition persists if one thread sets a
> reference count to zero and another decrements it to zero, both may
> attempt to delete the same piece of memory. A true pain to debug.
You're right, we can still have a race condition. The following
version of SetReferenceCount() should do the correct job:
void
LightObject
::SetReferenceCount(int ref)
{
m_ReferenceCountLock.Lock();
m_ReferenceCount = ref;
m_ReferenceCountLock.Unlock();
if ( ref <= 0) // <--- changes here
{
delete this;
}
}
I'm rebuilding ITK with that change, and will commit it if all the
test are OK.
However, contrary to UnRegister(), I think we are talking about an
already highly dangerous method, which shouldn't be used excepted in
a few (rare) cases.
Do you have seen that problem in java wrappers?
Thanks a lot for your efforts to fix that problem.
Gaëtan
>
> void
> LightObject
> ::UnRegister() const
> {
> m_ReferenceCountLock.Lock();
> int tmpReferenceCount = --m_ReferenceCount;
> m_ReferenceCountLock.Unlock();
>
> // ReferenceCount in now unlocked. We may have a race condition
> // to delete the object.
> if ( tmpReferenceCount <= 0)
> {
> delete this;
> }
> }
>
>
> void
> LightObject
> ::SetReferenceCount(int ref)
> {
> m_ReferenceCountLock.Lock();
> m_ReferenceCount = ref;
> m_ReferenceCountLock.Unlock();
>
> if ( m_ReferenceCount <= 0)
> {
> delete this;
> }
> }
>
>
> _______________________________________________
> Insight-users mailing list
> Insight-users at itk.org
> http://www.itk.org/mailman/listinfo/insight-users
--
Gaëtan Lehmann
Biologie du Développement et de la Reproduction
INRA de Jouy-en-Josas (France)
tel: +33 1 34 65 29 66 fax: 01 34 65 29 09
http://voxel.jouy.inra.fr
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 186 bytes
Desc: =?ISO-8859-1?Q?Ceci_est_une_signature_=E9lectronique_PGP?=
Url : http://public.kitware.com/pipermail/insight-users/attachments/20071108/f0d18781/PGP.pgp
More information about the Insight-users
mailing list