diff options
author | Daniel Boelzle <dbo@openoffice.org> | 2003-07-15 14:42:35 +0000 |
---|---|---|
committer | Daniel Boelzle <dbo@openoffice.org> | 2003-07-15 14:42:35 +0000 |
commit | d1855243561bfe60074368b2d60ee122af1247eb (patch) | |
tree | 17e9e1f34d74a707aafe4201ca5b88f8aeda9de9 /cli_ure | |
parent | 86d237db4ea6b75217b2b44319430ac59cdc76d4 (diff) |
#107130# minor fixes
Diffstat (limited to 'cli_ure')
-rw-r--r-- | cli_ure/source/ure/uno/util/DisposeGuard.cs | 16 | ||||
-rw-r--r-- | cli_ure/source/ure/uno/util/WeakComponentBase.cs | 140 |
2 files changed, 116 insertions, 40 deletions
diff --git a/cli_ure/source/ure/uno/util/DisposeGuard.cs b/cli_ure/source/ure/uno/util/DisposeGuard.cs index 57916ec45a3b..7c124be1c8f6 100644 --- a/cli_ure/source/ure/uno/util/DisposeGuard.cs +++ b/cli_ure/source/ure/uno/util/DisposeGuard.cs @@ -2,9 +2,9 @@ * * $RCSfile: DisposeGuard.cs,v $ * - * $Revision: 1.4 $ + * $Revision: 1.5 $ * - * last change: $Author: dbo $ $Date: 2003-07-02 14:17:31 $ + * last change: $Author: dbo $ $Date: 2003-07-15 15:42:35 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -71,17 +71,9 @@ namespace uno.util public struct DisposeGuard : IDisposable { private XComponent m_xComponent; - - /** ctor. - - @param obj target object - */ - public DisposeGuard( Object obj ) - { - m_xComponent = (XComponent) obj; - } + /** ctor. - + @param obj target object */ public DisposeGuard( XComponent obj ) diff --git a/cli_ure/source/ure/uno/util/WeakComponentBase.cs b/cli_ure/source/ure/uno/util/WeakComponentBase.cs index 70be37dc4429..dc5d91dd70ce 100644 --- a/cli_ure/source/ure/uno/util/WeakComponentBase.cs +++ b/cli_ure/source/ure/uno/util/WeakComponentBase.cs @@ -2,9 +2,9 @@ * * $RCSfile: WeakComponentBase.cs,v $ * - * $Revision: 1.2 $ + * $Revision: 1.3 $ * - * last change: $Author: dbo $ $Date: 2003-04-07 09:40:46 $ + * last change: $Author: dbo $ $Date: 2003-07-15 15:42:35 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -73,40 +73,126 @@ namespace uno.util In addition, it implements the interface unoidl.com.sun.star.lang.XComponent to be disposed explicitly. */ -public class WeakComponentBase : WeakBase, XComponent, IDisposable +public class WeakComponentBase : WeakBase, XComponent { private delegate void t_disposing( EventObject evt ); private t_disposing m_disposing = null; + private bool m_inDispose = false; + private bool m_disposed = false; + + /** Indicates whether object is alrady disposed. + + @return + true, if object has been disposed + */ + protected bool isDisposed() + { + lock (this) + { + return m_disposed; + } + } + + /** Checks whether this object is disposed and throws a DisposedException + if it is already disposed. + */ + protected void checkUnDisposed() + { + if (! isDisposed()) + { + throw new unoidl.com.sun.star.lang.DisposedException( + "object already disposed!", this ); + } + } + + ~WeakComponentBase() + { + bool doDispose; + lock (this) + { + doDispose = (!m_inDispose && !m_disposed); + } + if (doDispose) + { + dispose(); + } + } + + /** Override to perform extra clean-up work. Provided for subclasses. + It is called during dispose() + */ + protected void preDisposing() + { + } - /** Disposing callback called when the object is being disposed. - This method has to be implemented by sub classes. + /** Override to become notified right before the disposing action is + performed. */ - protected virtual void disposing() + protected void postDisposing() { } // XComponent impl /** This method is called by the owner of this object to explicitly - dispose it. This implementation of dispose() first notifies all - registered event listeners and finally this object by calling its - protected disposing(). + dispose it. This implementation of dispose() first notifies this object + via preDisposing(), then all registered event listeners and + finally this object again calling postDisposing(). */ public void dispose() { - // send disposing notifications to listeners - t_disposing call; + // Determine in a thread-safe way if this is the first call to this + // method. Only then we proceed with the notification of event + // listeners. It is an error to call this method more then once. + bool doDispose = false; + t_disposing call = null; lock (this) { - call = m_disposing; - m_disposing = null; + if (! m_inDispose && !m_disposed) + { + call = m_disposing; + m_disposing = null; + m_inDispose = true; + doDispose = true; + } + } + // The notification occures in an unsynchronized block in order to avoid + // deadlocks if one of the listeners calls back in a different thread on + // a synchronized method which uses the same object. + if (doDispose) + { + try + { + // call sub class + preDisposing(); + // send disposing notifications to listeners + if (null != call) + { + EventObject evt = new EventObject( this ); + call( evt ); + } + // call sub class + postDisposing(); + } + finally + { + // finally makes sure that the flags are set ensuring + // that this function is only called once. + m_disposed = true; + m_inDispose = false; + } } - if (null != call) + else { - EventObject evt = new EventObject( this ); - call( evt ); + // in a multithreaded environment, it can't be avoided, + // that dispose is called twice. + // However this condition is traced, because it MAY indicate an + // error. +#if DEBUG + Console.WriteLine( + "WeakComponentBase.dispose() - dispose called twice" ); +#endif +// Debug.Fail( "WeakComponentBase.dispose() - dispose called twice" ); } - // call sub class - disposing(); } /** Registers an event listener being notified when this object is disposed. @@ -114,10 +200,15 @@ public class WeakComponentBase : WeakBase, XComponent, IDisposable */ public void addEventListener( XEventListener xListener ) { + bool add; lock (this) { - m_disposing += new t_disposing( xListener.disposing ); + add = (! m_inDispose && !m_disposed); + if (add) + m_disposing += new t_disposing( xListener.disposing ); } + if (! add) + xListener.disposing( new EventObject( this ) ); } /** Revokes an event listener from being notified when this object is disposed. @@ -128,17 +219,10 @@ public class WeakComponentBase : WeakBase, XComponent, IDisposable { lock (this) { - m_disposing -= new t_disposing( xListener.disposing ); + if (! m_inDispose && !m_disposed) + m_disposing -= new t_disposing( xListener.disposing ); } } - - /** System.IDisposable implementation to conveniently auto dispose - UNO objects within CLI code. - */ - public void Dispose() - { - dispose(); - } } } |