Exemple #1
0
static void
example()
{

  C* ptr =  new C();

  // Get weak pointers to ptr. The first time asWeakPtr is called
  // a reference counted WeakReference object is created that
  // can live beyond the lifetime of 'ptr'. The WeakReference
  // object will be notified of 'ptr's destruction.
  WeakPtr<C> weak = ptr->asWeakPtr();
  WeakPtr<C> other = ptr->asWeakPtr();

  // Test a weak pointer for validity before using it.
  if (weak) {
    weak->num = 17;
    weak->act();
  }

  // Destroying the underlying object clears weak pointers to it.
  delete ptr;

  MOZ_ASSERT(!weak, "Deleting |ptr| clears weak pointers to it.");
  MOZ_ASSERT(!other, "Deleting |ptr| clears all weak pointers to it.");
}