如何测试ARC下弱引用是否可以使用Objective-C对象?

问题描述 投票:4回答:1

Apple提到了ARC的发行说明中记录的两种方法supportsWeakPointers,但在实际的运行时和框架中从未提及。还可以观察到,此supportsWeakPointers的另一种方法是method is in fact ignored in practice.,该方法未在任何地方记录,但在allowsWeakReference中声明如下。

NSObject.h

在运行时尝试调用- (BOOL)allowsWeakReference NS_UNAVAILABLE; - (BOOL)retainWeakReference NS_UNAVAILABLE; 会导致程序由于以下堆栈跟踪而崩溃

allowsWeakReference

因此,如果两种方法都不能使用,那么如何测试对象是否支持对它的弱引用的形成?

objective-c cocoa automatic-ref-counting weak-references
1个回答
1
投票

最终一起破解了。适用于我需要的东西。

objc[17337]: Do not call -_isDeallocating.

#0  0x00007fff9900f768 in _objc_trap ()
#1  0x00007fff9900f8aa in _objc_fatal ()
#2  0x00007fff9901bd90 in _objc_rootIsDeallocating ()
#3  0x00007fff97e92ce9 in -[NSObject _isDeallocating] ()
#4  0x00007fff97b5fad5 in -[NSObject(NSObject) allowsWeakReference] ()
#5  0x00007fff97dfe021 in -[NSObject performSelector:] ()
...
...
#11 0x00007fff97a5fd32 in __-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke_1 ()
#12 0x00007fff97dafaaa in _CFXNotificationPost ()
#13 0x00007fff97a4bfe7 in -[NSNotificationCenter postNotificationName:object:userInfo:] ()
#14 0x00007fff8fa0460f in -[NSApplication _postDidFinishNotification] ()
#15 0x00007fff8fa04375 in -[NSApplication _sendFinishLaunchingNotification] ()
#16 0x00007fff8fa0303c in -[NSApplication(NSAppleEventHandling) _handleAEOpenEvent:] ()
#17 0x00007fff8fa02d9d in -[NSApplication(NSAppleEventHandling) _handleCoreEvent:withReplyEvent:] ()
#18 0x00007fff97df9591 in -[NSObject performSelector:withObject:withObject:] ()
#19 0x00007fff97a827eb in __-[NSAppleEventManager setEventHandler:andSelector:forEventClass:andEventID:]_block_invoke_1 ()
#20 0x00007fff97a81772 in -[NSAppleEventManager dispatchRawAppleEvent:withRawReply:handlerRefCon:] ()
#21 0x00007fff97a81600 in _NSAppleEventManagerGenericHandler ()
#22 0x00007fff96623c25 in aeDispatchAppleEvent ()
#23 0x00007fff96623b03 in dispatchEventAndSendReply ()
#24 0x00007fff966239f7 in aeProcessAppleEvent ()
#25 0x00007fff92101af9 in AEProcessAppleEvent ()
#26 0x00007fff8fa001a9 in _DPSNextEvent ()
#27 0x00007fff8f9ff861 in -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] ()
#28 0x00007fff8f9fc19d in -[NSApplication run] ()
#29 0x00007fff8fc7ab88 in NSApplicationMain ()
© www.soinside.com 2019 - 2024. All rights reserved.