XComponent

From Apache OpenOffice Wiki
Jump to: navigation, search



If the implementation holds a reference to another UNO object internally, there may be a problem of cyclic references that might prevent your component and the other object from being destroyed forever. If it is probable that the other object may hold a reference to your component, implement com.sun.star.lang.XComponent that contains a method dispose(). Chapter Lifetime of UNO Objects discusses the intricacies of this issue.

Supporting XComponent in a C++ or Java component is simple, because there are helper classes to derive from that implement XComponent. The following code is an example if you must implement XComponent manually.

The interface XComponent specifies these operations:

  // module com::sun::star::lang
 
  interface XComponent: com::sun::star::uno::XInterface
  { 
      void dispose(); 
      void addEventListener( [in] XEventListener xListener ); 
      void removeEventListener( [in] XEventListener aListener ); 
  };

XComponent uses the interface com.sun.star.lang.XEventListener:

  // module com::sun::star::lang
  interface XEventListener: com::sun::star::uno::XInterface
  { 
      void disposing( [in] com::sun::star::lang::EventObject Source ); 
  };

Disposing of an XComponent

The idea behind XComponent is that the object is instantiated by a third object that makes the third object the owner of first object. The owner is allowed to call dispose(). When the owner calls dispose() at your object, it must do three things:

  • Release all references it holds.
  • Inform registered XEventListeners that it is being disposed of by calling their method disposing().
  • Behave as passive as possible afterwards. If the implementation is called after being disposed, throw a com.sun.star.lang.DisposedException if you cannot fulfill the method specification.

That way the owner of XComponent objects can dissolve a possible cyclic reference.

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages