summaryrefslogtreecommitdiff
path: root/mfbt/tests/TestWeakPtr.cpp
blob: db683aca21738757859337cc53ee18df94c257f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/WeakPtr.h"

using mozilla::SupportsWeakPtr;
using mozilla::WeakPtr;

// To have a class C support weak pointers, inherit from SupportsWeakPtr<C>.
class C : public SupportsWeakPtr<C>
{
public:
  MOZ_DECLARE_WEAKREFERENCE_TYPENAME(C)

  int mNum;

  C()
    : mNum(0)
  {}

  ~C()
  {
    // Setting mNum in the destructor allows us to test against use-after-free below
    mNum = 0xDEAD;
  }

  void act() {}

  bool isConst() {
    return false;
  }

  bool isConst() const {
    return true;
  }
};

bool isConst(C*)
{
  return false;
}

bool isConst(const C*)
{
  return true;
}

int
main()
{
  C* c1 = new C;
  MOZ_RELEASE_ASSERT(c1->mNum == 0);

  // Get weak pointers to c1. The first time,
  // a reference-counted WeakReference object is created that
  // can live beyond the lifetime of 'c1'. The WeakReference
  // object will be notified of 'c1's destruction.
  WeakPtr<C> w1 = c1;
  // Test a weak pointer for validity before using it.
  MOZ_RELEASE_ASSERT(w1);
  MOZ_RELEASE_ASSERT(w1 == c1);
  w1->mNum = 1;
  w1->act();

  // Test taking another WeakPtr<C> to c1
  WeakPtr<C> w2 = c1;
  MOZ_RELEASE_ASSERT(w2);
  MOZ_RELEASE_ASSERT(w2 == c1);
  MOZ_RELEASE_ASSERT(w2 == w1);
  MOZ_RELEASE_ASSERT(w2->mNum == 1);

  // Test a WeakPtr<const C>
  WeakPtr<const C> w3const = c1;
  MOZ_RELEASE_ASSERT(w3const);
  MOZ_RELEASE_ASSERT(w3const == c1);
  MOZ_RELEASE_ASSERT(w3const == w1);
  MOZ_RELEASE_ASSERT(w3const == w2);
  MOZ_RELEASE_ASSERT(w3const->mNum == 1);

  // Test const-correctness of operator-> and operator T*
  MOZ_RELEASE_ASSERT(!w1->isConst());
  MOZ_RELEASE_ASSERT(w3const->isConst());
  MOZ_RELEASE_ASSERT(!isConst(w1));
  MOZ_RELEASE_ASSERT(isConst(w3const));

  // Test that when a WeakPtr is destroyed, it does not destroy the object that it points to,
  // and it does not affect other WeakPtrs pointing to the same object (e.g. it does not
  // destroy the WeakReference object).
  {
    WeakPtr<C> w4local = c1;
    MOZ_RELEASE_ASSERT(w4local == c1);
  }
  // Now w4local has gone out of scope. If that had destroyed c1, then the following would fail
  // for sure (see C::~C()).
  MOZ_RELEASE_ASSERT(c1->mNum == 1);
  // Check that w4local going out of scope hasn't affected other WeakPtr's pointing to c1
  MOZ_RELEASE_ASSERT(w1 == c1);
  MOZ_RELEASE_ASSERT(w2 == c1);

  // Now construct another C object and test changing what object a WeakPtr points to
  C* c2 = new C;
  c2->mNum = 2;
  MOZ_RELEASE_ASSERT(w2->mNum == 1); // w2 was pointing to c1
  w2 = c2;
  MOZ_RELEASE_ASSERT(w2);
  MOZ_RELEASE_ASSERT(w2 == c2);
  MOZ_RELEASE_ASSERT(w2 != c1);
  MOZ_RELEASE_ASSERT(w2 != w1);
  MOZ_RELEASE_ASSERT(w2->mNum == 2);

  // Destroying the underlying object clears weak pointers to it.
  // It should not affect pointers that are not currently pointing to it.
  delete c1;
  MOZ_RELEASE_ASSERT(!w1, "Deleting an object should clear WeakPtr's to it.");
  MOZ_RELEASE_ASSERT(!w3const, "Deleting an object should clear WeakPtr's to it.");
  MOZ_RELEASE_ASSERT(w2, "Deleting an object should not clear WeakPtr that are not pointing to it.");

  delete c2;
  MOZ_RELEASE_ASSERT(!w2, "Deleting an object should clear WeakPtr's to it.");
}