TestMainThreadWeakPtr.cpp (987B)
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 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "gtest/gtest.h" 8 9 #include "mozilla/WeakPtr.h" 10 #include "mozilla/UniquePtr.h" 11 #include <thread> 12 13 using namespace mozilla; 14 15 struct C : public SupportsWeakPtr { 16 int mNum{0}; 17 }; 18 19 struct HasWeakPtrToC { 20 explicit HasWeakPtrToC(C* c) : mPtr(c) {} 21 22 MainThreadWeakPtr<C> mPtr; 23 24 ~HasWeakPtrToC() { 25 MOZ_RELEASE_ASSERT(!NS_IsMainThread(), "Should be released OMT"); 26 } 27 }; 28 29 TEST(MFBT_MainThreadWeakPtr, Basic) 30 { 31 auto c = MakeUnique<C>(); 32 MOZ_RELEASE_ASSERT(NS_IsMainThread()); 33 34 auto weakRef = MakeUnique<HasWeakPtrToC>(c.get()); 35 36 std::thread t([weakRef = std::move(weakRef)] {}); 37 38 MOZ_RELEASE_ASSERT(!weakRef); 39 c = nullptr; 40 41 t.join(); 42 }