acquire() 和 release()

From Apache OpenOffice Wiki
Jump to: navigation, search



每个 UNO 接口都是从 com.sun.star.uno.XInterface 派生的:

  // module com::sun::star::uno
  interface XInterface
  {
      any queryInterface( [in] type aType ); 
      [oneway] void acquire(); 
      [oneway] void release(); 
  };

UNO 对象必须维护一个内部引用计数器。调用 UNO 接口的 acquire() 会使引用计数增加 1。调用 UNO 接口的 release() 会使引用计数减少 1。如果引用计数减少到零,则 UNO 对象可能被析构。对象析构有时称为对象死亡。一个对象的引用计数必须总是为非负数。


一旦对某个 UNO 对象调用 acquire(),就会出现对该对象的引用硬引用(与弱引用相对)。对该对象调用 release() 通常称为释放清除该引用。


UNO 对象不导出引用计数的状态,也就是说,acquire()release() 没有返回值。一般来说,UNO 对象不应该对引用计数的具体值进行任何假设,从 1 转变到 0 除外。


如果以前已经调用了 acquire (),就允许首次调用一个方法。对于每次 acquire() 调用,必须进行相应的 release 调用,否则对象就会泄漏。


Documentation note.png 如果以前已经调用了 acquire (),就允许首次调用一个方法。每次调用 acquire() 时,必须进行相应的 release 调用,否则对象就会泄漏。UNO Java 绑定将 acquire()release() 封装在 UnoRuntime.queryInterface() 调用中。这同样适用于 C++ 中的 Reference<> 模板。只要是通过这些机制获取接口引用,就无需在程序中调用 acquire()release()


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