1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/RefPtr.h"
8 #include "mozilla/RefCounted.h"
10 using mozilla::RefCounted
;
12 class Foo
: public RefCounted
<Foo
>
15 MOZ_DECLARE_REFCOUNTED_TYPENAME(Foo
)
17 Foo() : mDead(false) {}
19 static int sNumDestroyed
;
31 int Foo::sNumDestroyed
;
33 struct Bar
: public Foo
{};
38 RefPtr
<Foo
> f(new Foo());
45 RefPtr
<Bar
> bar
= new Bar();
53 // Kids, don't try this at home
58 GetNewFoo(RefPtr
<Foo
>* aFoo
)
72 MOZ_RELEASE_ASSERT(0 == Foo::sNumDestroyed
);
74 RefPtr
<Foo
> f
= new Foo();
75 MOZ_RELEASE_ASSERT(f
->refCount() == 1);
77 MOZ_RELEASE_ASSERT(1 == Foo::sNumDestroyed
);
80 RefPtr
<Foo
> f1
= NewFoo();
81 RefPtr
<Foo
> f2(NewFoo());
82 MOZ_RELEASE_ASSERT(1 == Foo::sNumDestroyed
);
84 MOZ_RELEASE_ASSERT(3 == Foo::sNumDestroyed
);
87 RefPtr
<Foo
> b
= NewBar();
88 MOZ_RELEASE_ASSERT(3 == Foo::sNumDestroyed
);
90 MOZ_RELEASE_ASSERT(4 == Foo::sNumDestroyed
);
98 MOZ_RELEASE_ASSERT(4 == Foo::sNumDestroyed
);
100 MOZ_RELEASE_ASSERT(4 == Foo::sNumDestroyed
);
102 MOZ_RELEASE_ASSERT(5 == Foo::sNumDestroyed
);
106 RefPtr
<Foo
> f
= new Foo();
107 RefPtr
<Foo
> g
= f
.forget();
109 MOZ_RELEASE_ASSERT(6 == Foo::sNumDestroyed
);
113 RefPtr
<Foo
> f
= new Foo();
114 GetNewFoo(getter_AddRefs(f
));
115 MOZ_RELEASE_ASSERT(7 == Foo::sNumDestroyed
);
117 MOZ_RELEASE_ASSERT(8 == Foo::sNumDestroyed
);
120 RefPtr
<Foo
> f
= new Foo();
122 MOZ_RELEASE_ASSERT(9 == Foo::sNumDestroyed
);
124 MOZ_RELEASE_ASSERT(10 == Foo::sNumDestroyed
);
127 RefPtr
<Foo
> f1
= new Bar();
129 MOZ_RELEASE_ASSERT(11 == Foo::sNumDestroyed
);
132 RefPtr
<Foo
> f
= GetNullFoo();
133 MOZ_RELEASE_ASSERT(11 == Foo::sNumDestroyed
);
135 MOZ_RELEASE_ASSERT(11 == Foo::sNumDestroyed
);