Flutter Engine
The Flutter Engine
weak_nsobject.mm
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "flutter/fml/platform/darwin/weak_nsobject.h"
6#include "flutter/fml/platform/darwin/scoped_nsautorelease_pool.h"
7#include "flutter/fml/platform/darwin/scoped_nsobject.h"
8
9namespace {
10// The key needed by objc_setAssociatedObject.
11char sentinelObserverKey_;
12} // namespace
13
14namespace fml {
15
17 : object_(object), checker_(checker) {}
18
19WeakContainer::~WeakContainer() {}
20
21} // namespace fml
22
23@interface CRBWeakNSProtocolSentinel ()
24// Container to notify on dealloc.
25@property(readonly, assign) fml::RefPtr<fml::WeakContainer> container;
26// Designed initializer.
27- (id)initWithContainer:(fml::RefPtr<fml::WeakContainer>)container;
28@end
29
30@implementation CRBWeakNSProtocolSentinel
31
32+ (fml::RefPtr<fml::WeakContainer>)containerForObject:(id)object
33 threadChecker:(debug::DebugThreadChecker)checker {
34 if (object == nil) {
35 return nullptr;
36 }
37 // The autoreleasePool is needed here as the call to objc_getAssociatedObject
38 // returns an autoreleased object which is better released sooner than later.
40 CRBWeakNSProtocolSentinel* sentinel = objc_getAssociatedObject(object, &sentinelObserverKey_);
41 if (!sentinel) {
43 initWithContainer:AdoptRef(new fml::WeakContainer(object, checker))]);
44 sentinel = newSentinel;
45 objc_setAssociatedObject(object, &sentinelObserverKey_, sentinel, OBJC_ASSOCIATION_RETAIN);
46 // The retain count is 2. One retain is due to the alloc, the other to the
47 // association with the weak object.
48 FML_DCHECK(2u == [sentinel retainCount]);
49 }
50 return [sentinel container];
51}
52
53- (id)initWithContainer:(fml::RefPtr<fml::WeakContainer>)container {
55 self = [super init];
56 if (self) {
57 _container = container;
58 }
59 return self;
60}
61
62- (void)dealloc {
63 _container->nullify();
64 [super dealloc];
65}
66
67@end
AutoreleasePool pool
T * get() const
Definition: ref_ptr.h:116
WeakContainer(id object, const debug::DebugThreadChecker &checker)
#define FML_DCHECK(condition)
Definition: logging.h:103
fml::RefPtr< fml::WeakContainer > container
Definition: ascii_trie.cc:9
RefPtr< T > AdoptRef(T *ptr)
Definition: ref_ptr.h:222
const uintptr_t id