[Insight-developers] LightObject race condition fix
Gaëtan Lehmann
gaetan.lehmann at jouy.inra.fr
Tue Jul 10 14:51:21 EDT 2007
Hi,
Julia Smith has reported a weird race conditions some days ago,
discovered while using java. I have patched the LightObject's code to
avoid it, while changing as less as possible the code to avoid
involuntary breakage.
Index: Code/Common/itkLightObject.cxx
===================================================================
RCS file: /cvsroot/Insight/Insight/Code/Common/itkLightObject.cxx,v
retrieving revision 1.34
diff -u -r1.34 itkLightObject.cxx
--- Code/Common/itkLightObject.cxx 4 Apr 2007 20:04:10
-0000 1.34
+++ Code/Common/itkLightObject.cxx 10 Jul 2007 18:41:08 -0000
@@ -150,12 +150,12 @@
::UnRegister() const
{
m_ReferenceCountLock.Lock();
- m_ReferenceCount--;
+ int tmpReferenceCount = --m_ReferenceCount;
m_ReferenceCountLock.Unlock();
// ReferenceCount in now unlocked. We may have a race condition
// to delete the object.
- if ( m_ReferenceCount <= 0)
+ if ( tmpReferenceCount <= 0)
{
delete this;
}
Index: Code/Common/itkLightObject.h
===================================================================
RCS file: /cvsroot/Insight/Insight/Code/Common/itkLightObject.h,v
retrieving revision 1.33
diff -u -r1.33 itkLightObject.h
--- Code/Common/itkLightObject.h 6 Feb 2006 22:01:56
-0000 1.33
+++ Code/Common/itkLightObject.h 10 Jul 2007 18:41:08 -0000
@@ -114,7 +114,7 @@
virtual void PrintTrailer(std::ostream& os, Indent indent) const;
/** Number of uses of this object by other objects. */
- mutable int m_ReferenceCount;
+ volatile mutable int m_ReferenceCount;
/** Mutex lock to protect modification to the reference count */
mutable SimpleFastMutexLock m_ReferenceCountLock;
All the tests are OK.
If no one disagree, I'd like to put the changes in the repository.
Julia has proposed more changes to unsure nothing bad can be done,
like attempting to delete an object twice.
I think they should be reviewed and (hopefully) adopted at the
beginning of the next release cycle.
Gaëtan
--
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://www.itk.org/mailman/private/insight-developers/attachments/20070710/b30ace0c/PGP.pgp
More information about the Insight-developers
mailing list