LibreOffice Module cppuhelper (master)  1
weak.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <sal/config.h>
21 #include <sal/log.hxx>
22 
23 #include <osl/diagnose.h>
24 #include <osl/mutex.hxx>
25 #include <cppuhelper/weakagg.hxx>
26 #include <cppuhelper/exc_hlp.hxx>
28 
29 #include <com/sun/star/lang/DisposedException.hpp>
30 
31 #include <algorithm>
32 #include <vector>
33 
34 using namespace osl;
35 using namespace com::sun::star::uno;
36  //for docpp
38 namespace cppu
39 {
40 
41 // due to static Reflection destruction from usr, there must be a mutex leak (#73272#)
42 // this is used to lock all instances of OWeakConnectionPoint and OWeakRefListener as well as OWeakObject::m_pWeakConnectionPoint
43 static Mutex & getWeakMutex()
44 {
45  static Mutex * s_pMutex = new Mutex();
46  return *s_pMutex;
47 }
48 
49 
50 //-- OWeakConnectionPoint ----------------------------------------------------
51 
52 class OWeakConnectionPoint: public XAdapter
53 {
54 public:
59  : m_aRefCount( 0 )
60  , m_pObject(pObj)
61  {}
62 
63  // noncopyable
65  const OWeakConnectionPoint& operator=(const OWeakConnectionPoint&) = delete;
66 
67  // XInterface
68  Any SAL_CALL queryInterface( const Type & rType ) override;
69  void SAL_CALL acquire() throw() override;
70  void SAL_CALL release() throw() override;
71 
72  // XAdapter
73  css::uno::Reference< css::uno::XInterface > SAL_CALL queryAdapted() override;
74  void SAL_CALL addReference( const css::uno::Reference< css::uno::XReference >& xRef ) override;
75  void SAL_CALL removeReference( const css::uno::Reference< css::uno::XReference >& xRef ) override;
76 
80  void dispose();
81 
82 private:
83  virtual ~OWeakConnectionPoint() {}
84 
86  oslInterlockedCount m_aRefCount;
90  std::vector<Reference<XReference>> m_aReferences;
91 };
92 
93 // XInterface
95 {
97  rType, static_cast< XAdapter * >( this ), static_cast< XInterface * >( this ) );
98 }
99 
100 // XInterface
101 void SAL_CALL OWeakConnectionPoint::acquire() throw()
102 {
103  osl_atomic_increment( &m_aRefCount );
104 }
105 
106 // XInterface
107 void SAL_CALL OWeakConnectionPoint::release() throw()
108 {
109  if (! osl_atomic_decrement( &m_aRefCount ))
110  delete this;
111 }
112 
113 void OWeakConnectionPoint::dispose()
114 {
115  std::vector<Reference<XReference>> aCopy;
116  { // only hold the mutex while we access the field
117  MutexGuard aGuard(getWeakMutex());
118  // OWeakObject is not the only owner of this, so clear m_pObject
119  // so that queryAdapted() won't use it now that it's dead
120  m_pObject = nullptr;
121  // other code is going to call removeReference while we are doing this, so we need a
122  // copy, but since we are disposing and going away, we can just take the original data
123  aCopy.swap(m_aReferences);
124  }
125  Any ex;
126  for (const Reference<XReference> & i : aCopy )
127  {
128  try
129  {
130  i->dispose();
131  }
132  catch (css::lang::DisposedException &) {}
133  catch (RuntimeException &)
134  {
136  }
137  }
138  if (ex.hasValue())
139  {
141  }
142 }
143 
144 // XInterface
145 Reference< XInterface > SAL_CALL OWeakConnectionPoint::queryAdapted()
146 {
148 
149  ClearableMutexGuard guard(getWeakMutex());
150 
151  if (m_pObject)
152  {
153  oslInterlockedCount n = osl_atomic_increment( &m_pObject->m_refCount );
154 
155  if (n > 1)
156  {
157  // The reference is incremented. The object cannot be destroyed.
158  // Release the guard at the earliest point.
159  guard.clear();
160  // WeakObject has a (XInterface *) cast operator
161  ret = *m_pObject;
162  osl_atomic_decrement( &m_pObject->m_refCount );
163  }
164  else
165  // Another thread wait in the dispose method at the guard
166  osl_atomic_decrement( &m_pObject->m_refCount );
167  }
168 
169  return ret;
170 }
171 
172 // XInterface
173 void SAL_CALL OWeakConnectionPoint::addReference(const Reference< XReference >& rRef)
174 {
175  MutexGuard aGuard(getWeakMutex());
176  m_aReferences.push_back( rRef );
177 }
178 
179 // XInterface
180 void SAL_CALL OWeakConnectionPoint::removeReference(const Reference< XReference >& rRef)
181 {
182  MutexGuard aGuard(getWeakMutex());
183  // Search from end because the thing that last added a ref is most likely to be the
184  // first to remove a ref.
185  // It's not really valid to compare the pointer directly, but it's faster.
186  auto it = std::find_if(m_aReferences.rbegin(), m_aReferences.rend(),
187  [&rRef](const Reference<XReference>& rxRef) { return rxRef.get() == rRef.get(); });
188  if (it != m_aReferences.rend()) {
189  m_aReferences.erase( it.base()-1 );
190  return;
191  }
192  // interface not found, use the correct compare method
193  it = std::find(m_aReferences.rbegin(), m_aReferences.rend(), rRef);
194  if ( it != m_aReferences.rend() )
195  m_aReferences.erase( it.base()-1 );
196 }
197 
198 
199 //-- OWeakObject -------------------------------------------------------
200 
201 
202 #ifdef _MSC_VER
203 // Accidentally occurs in msvc mapfile = > had to be outlined.
204 OWeakObject::OWeakObject()
205  : m_refCount( 0 ),
206  m_pWeakConnectionPoint( nullptr )
207 {
208 }
209 #endif
210 
211 // XInterface
212 Any SAL_CALL OWeakObject::queryInterface( const Type & rType )
213 {
215  rType,
216  static_cast< XWeak * >( this ), static_cast< XInterface * >( this ) );
217 }
218 
219 // XInterface
220 void SAL_CALL OWeakObject::acquire() throw()
221 {
222  osl_atomic_increment( &m_refCount );
223 }
224 
225 // XInterface
226 void SAL_CALL OWeakObject::release() throw()
227 {
228  if (osl_atomic_decrement( &m_refCount ) == 0) {
229  // notify/clear all weak-refs before object's dtor is executed
230  // (which may check weak-refs to this object):
232  // destroy object:
233  delete this;
234  }
235 }
236 
238 {
239  OSL_PRECOND( m_refCount == 0, "OWeakObject::disposeWeakConnectionPoint: only to be called with a ref count of 0!" );
240  if (m_pWeakConnectionPoint != nullptr) {
241  OWeakConnectionPoint * const p = m_pWeakConnectionPoint;
242  m_pWeakConnectionPoint = nullptr;
243  try {
244  p->dispose();
245  }
246  catch (RuntimeException const& exc) {
247  SAL_WARN( "cppuhelper", exc );
248  }
249  p->release();
250  }
251 }
252 
253 OWeakObject::~OWeakObject() COVERITY_NOEXCEPT_FALSE
254 {
255 }
256 
257 // XWeak
259 {
260  if (!m_pWeakConnectionPoint)
261  {
262  // only acquire mutex if member is not created
263  MutexGuard aGuard( getWeakMutex() );
264  if( !m_pWeakConnectionPoint )
265  {
267  p->acquire();
268  m_pWeakConnectionPoint = p;
269  }
270  }
271 
272  return m_pWeakConnectionPoint;
273 }
274 
275 
276 //-- OWeakAggObject ----------------------------------------------------
277 
279 {
280 }
281 
282 // XInterface
284 {
286  if (x.is())
287  x->acquire();
288  else
290 }
291 
292 // XInterface
294 {
296  if (x.is())
297  x->release();
298  else
300 }
301 
302 // XInterface
304 {
305  Reference< XInterface > x( xDelegator ); // harden ref
306  return (x.is() ? x->queryInterface( rType ) : queryAggregation( rType ));
307 }
308 
309 // XAggregation
311 {
313  rType,
314  static_cast< XInterface * >( static_cast< OWeakObject * >( this ) ),
315  static_cast< XAggregation * >( this ),
316  static_cast< XWeak * >( this ) );
317 }
318 
319 // XAggregation
321 {
322  xDelegator = rDelegator;
323 }
324 
325 }
326  //for docpp
328 namespace com::sun::star::uno
329 {
330 
331 
332 //-- OWeakRefListener -----------------------------------------------------
333 
334 class OWeakRefListener final : public XReference
335 {
336 public:
337  explicit OWeakRefListener(const Reference< XInterface >& xInt);
338  virtual ~OWeakRefListener();
339 
340  // noncopyable
341  OWeakRefListener(const OWeakRefListener&) = delete;
342  const OWeakRefListener& operator=(const OWeakRefListener&) = delete;
343 
344  // XInterface
345  Any SAL_CALL queryInterface( const Type & rType ) override;
346  void SAL_CALL acquire() throw() override;
347  void SAL_CALL release() throw() override;
348 
349  // XReference
350  void SAL_CALL dispose() override;
351 
353  oslInterlockedCount m_aRefCount;
356 };
357 
359  : m_aRefCount( 1 )
360 {
361  try
362  {
364 
365  if (xWeak.is())
366  {
367  m_XWeakConnectionPoint = xWeak->queryAdapter();
368 
369  if (m_XWeakConnectionPoint.is())
370  {
371  m_XWeakConnectionPoint->addReference(static_cast<XReference*>(this));
372  }
373  }
374  }
375  catch (RuntimeException &) { OSL_ASSERT( false ); } // assert here, but no unexpected()
376  osl_atomic_decrement( &m_aRefCount );
377 }
378 
380 {
381  try
382  {
383  if (m_XWeakConnectionPoint.is())
384  {
385  acquire(); // don't die again
386  m_XWeakConnectionPoint->removeReference(static_cast<XReference*>(this));
387  }
388  }
389  catch (RuntimeException &) { OSL_ASSERT( false ); } // assert here, but no unexpected()
390 }
391 
392 // XInterface
393 Any SAL_CALL OWeakRefListener::queryInterface( const Type & rType )
394 {
396  rType, static_cast< XReference * >( this ), static_cast< XInterface * >( this ) );
397 }
398 
399 // XInterface
400 void SAL_CALL OWeakRefListener::acquire() throw()
401 {
402  osl_atomic_increment( &m_aRefCount );
403 }
404 
405 // XInterface
406 void SAL_CALL OWeakRefListener::release() throw()
407 {
408  if( ! osl_atomic_decrement( &m_aRefCount ) )
409  delete this;
410 }
411 
413 {
415  {
416  MutexGuard guard(cppu::getWeakMutex());
417  if( m_XWeakConnectionPoint.is() )
418  {
419  xAdp = m_XWeakConnectionPoint;
420  m_XWeakConnectionPoint.clear();
421  }
422  }
423 
424  if( xAdp.is() )
425  xAdp->removeReference(static_cast<XReference*>(this));
426 }
427 
428 
429 //-- WeakReferenceHelper ----------------------------------------------------------
430 
432  : m_pImpl( nullptr )
433 {
434  if (xInt.is())
435  {
436  m_pImpl = new OWeakRefListener(xInt);
437  m_pImpl->acquire();
438  }
439 }
440 
442  : m_pImpl( nullptr )
443 {
444  Reference< XInterface > xInt( rWeakRef.get() );
445  if (xInt.is())
446  {
447  m_pImpl = new OWeakRefListener(xInt);
448  m_pImpl->acquire();
449  }
450 }
451 
453 {
454  try
455  {
456  if (m_pImpl)
457  {
458  m_pImpl->dispose();
459  m_pImpl->release();
460  m_pImpl = nullptr;
461  }
462  }
463  catch (RuntimeException &) { OSL_ASSERT( false ); } // assert here, but no unexpected()
464 }
465 
467 {
468  if (this == &rWeakRef)
469  {
470  return *this;
471  }
472  Reference< XInterface > xInt( rWeakRef.get() );
473  return operator = ( xInt );
474 }
475 
477  WeakReferenceHelper && other)
478 {
479  clear();
480  std::swap(m_pImpl, other.m_pImpl);
481  return *this;
482 }
483 
484 WeakReferenceHelper & SAL_CALL
486 {
487  try
488  {
489  clear();
490  if (xInt.is())
491  {
492  m_pImpl = new OWeakRefListener(xInt);
493  m_pImpl->acquire();
494  }
495  }
496  catch (RuntimeException &) { OSL_ASSERT( false ); } // assert here, but no unexpected()
497  return *this;
498 }
499 
501 {
502  clear();
503 }
504 
506 {
507  try
508  {
510  {
511  // must lock to access m_XWeakConnectionPoint
512  MutexGuard guard(cppu::getWeakMutex());
513  if( m_pImpl && m_pImpl->m_XWeakConnectionPoint.is() )
514  xAdp = m_pImpl->m_XWeakConnectionPoint;
515  }
516 
517  if (xAdp.is())
518  return xAdp->queryAdapted();
519  }
520  catch (RuntimeException &)
521  {
522  OSL_ASSERT( false );
523  } // assert here, but no unexpected()
524 
525  return Reference< XInterface >();
526 }
527 
528 }
529 
530 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Type
const OWeakRefListener & operator=(const OWeakRefListener &)=delete
void SAL_CALL throwException(Any const &exc)
virtual css::uno::Any SAL_CALL queryAggregation(const css::uno::Type &rType) SAL_OVERRIDE
Called by the delegator or queryInterface.
Definition: weak.cxx:310
static Mutex & getWeakMutex()
Definition: weak.cxx:43
ULONG m_refCount
void SAL_CALL dispose() override
Definition: weak.cxx:412
void SAL_CALL acquire() override
Definition: weak.cxx:400
void SAL_CALL release() override
Definition: weak.cxx:107
friend class OWeakConnectionPoint
Definition: weak.hxx:45
sal_Int64 n
Any SAL_CALL queryInterface(const Type &rType) override
Definition: weak.cxx:393
float x
Reference< XAdapter > m_XWeakConnectionPoint
The connection point of the weak object, guarded by getWeakMutex()
Definition: weak.cxx:355
virtual void SAL_CALL release() SAL_OVERRIDE
If a delegator is set, then the delegators gets released.
Definition: weak.cxx:293
std::vector< Reference< XReference > > m_aReferences
The container to hold the weak references.
Definition: weak.cxx:90
virtual void SAL_CALL acquire() SAL_OVERRIDE
increasing m_refCount
Definition: weak.cxx:220
Any SAL_CALL getCaughtException()
Use this function to get the dynamic type of a caught C++-UNO exception; completes the above function...
WeakReferenceHelper &SAL_CALL operator=(const WeakReferenceHelper &rWeakRef)
Releases this reference and takes over rWeakRef.
Definition: weak.cxx:466
virtual ~OWeakAggObject() SAL_OVERRIDE
Virtual dtor.
Definition: weak.cxx:278
void SAL_CALL release() override
Definition: weak.cxx:406
int i
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType) SAL_OVERRIDE
If a delegator is set, then the delegator is queried for the demanded interface.
Definition: weak.cxx:303
virtual ~OWeakObject() COVERITY_NOEXCEPT_FALSE
Virtual dtor.
Definition: weak.cxx:253
The WeakReferenceHelper holds a weak reference to an object.
Definition: weakref.hxx:51
OWeakConnectionPoint(OWeakObject *pObj)
Hold the weak object without an acquire (only the pointer).
Definition: weak.cxx:58
void dispose()
Called from the weak object if the reference count goes to zero.
Definition: weak.cxx:113
Base class to implement a UNO object supporting weak references, i.e.
Definition: weak.hxx:43
void SAL_CALL acquire() override
Definition: weak.cxx:101
virtual void SAL_CALL acquire() SAL_OVERRIDE
If a delegator is set, then the delegators gets acquired.
Definition: weak.cxx:283
virtual css::uno::Reference< css::uno::XAdapter > SAL_CALL queryAdapter() SAL_OVERRIDE
XWeak::queryAdapter() implementation.
Definition: weak.cxx:258
oslInterlockedCount m_refCount
reference count.
Definition: weak.hxx:67
oslInterlockedCount m_aRefCount
The reference counter.
Definition: weak.cxx:353
oslInterlockedCount m_aRefCount
The reference counter.
Definition: weak.cxx:86
void SAL_CALL clear()
Releases this reference.
Definition: weak.cxx:452
void disposeWeakConnectionPoint()
disposes and resets m_pWeakConnectionPoint
Definition: weak.cxx:237
void * p
virtual void SAL_CALL release() SAL_OVERRIDE
decreasing m_refCount
Definition: weak.cxx:226
css::uno::Reference< css::uno::XInterface > SAL_CALL get() const
Gets a hard reference to the object.
Definition: weak.cxx:505
OWeakObject * m_pObject
The weak object.
Definition: weak.cxx:88
~WeakReferenceHelper()
Releases this reference.
Definition: weak.cxx:500
#define SAL_WARN(area, stream)
OWeakRefListener(const Reference< XInterface > &xInt)
Definition: weak.cxx:358
css::uno::WeakReferenceHelper xDelegator
weak reference to delegator.
Definition: weakagg.hxx:92
::std::unique_ptr< XmlIdRegistry_Impl > m_pImpl
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType) SAL_OVERRIDE
Basic queryInterface() implementation supporting com::sun::star::uno::XWeak and com::sun::star::uno::...
Definition: weak.cxx:212
css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType, Interface1 *p1)
Compares demanded type to given template argument types.
virtual void SAL_CALL setDelegator(const css::uno::Reference< css::uno::XInterface > &Delegator) SAL_OVERRIDE
Set the delegator.
Definition: weak.cxx:320