我想我找到了答案。 垃圾收集器的源代码似乎不存在, 但信头文件声明 < code> NSGarbage Colector 的界面, < code> NSGarbage Colector.h 包含以下内容: < code> Foundation. framework
// references outside the heap, globals, and the stack, e.g. unscanned memory, malloc memory, must be tracked by the collector
- (void)disableCollectorForPointer:(void *)ptr; // this pointer will not be collected...
- (void)enableCollectorForPointer:(void *)ptr; // ...until this (stacking) call is made
请注意“ 坚固” 的“ 堆积” < / 坚固” 评论 - 我想这意味着电话确实被计数了? 更多的证据仍然令人欢迎!
<强> 更新: 强>
我决定用一个小测试程序来测试我的假设(“强力”格克布里奇特.m 强) :
#import <Foundation/Foundation.h>
@interface PJGarbageCollectionTest : NSObject
@end
@implementation PJGarbageCollectionTest
- (id)init
{
self = [super init];
if (!self) return nil;
NSLog(@"%@ -init", self);
return self;
}
- (void)finalize
{
NSLog(@"%@ -finalize", self);
[super finalize];
}
@end
static void* ext_ptr1 = NULL;
static void* ext_ptr2 = NULL;
static void create()
{
PJGarbageCollectionTest* test = [[PJGarbageCollectionTest alloc] init];
[[NSGarbageCollector defaultCollector] disableCollectorForPointer:test];
ext_ptr1 = test;
[[NSGarbageCollector defaultCollector] disableCollectorForPointer:test];
ext_ptr2 = test;
}
static void killref(void** ext_ptr)
{
[[NSGarbageCollector defaultCollector] enableCollectorForPointer:*ext_ptr];
*ext_ptr = NULL;
}
int main()
{
NSLog(@"collector: %@", [NSGarbageCollector defaultCollector]);
create();
NSLog(@"Collecting with 2 external references");
[[NSGarbageCollector defaultCollector] collectExhaustively];
killref(&ext_ptr1);
NSLog(@"Collecting with 1 external reference");
[[NSGarbageCollector defaultCollector] collectExhaustively];
killref(&ext_ptr2);
NSLog(@"Collecting with 0 external references");
[[NSGarbageCollector defaultCollector] collectExhaustively];
return 0;
}
以 gcc -fobjc-gc- only -g -Wall -Wextra -ObjC gcbridgetest.m -Framework Foundation -o gcbridgetest
编译,以 ./gcbridgetest
运行,它提供以下输出,确认启用/禁用聚合器Forpointer:电话确实被计数 :
2012-06-12 16:08:08.278 gcbridgetest[29712:903] collector: <NSGarbageCollector: 0x20000f420>
2012-06-12 16:08:08.281 gcbridgetest[29712:903] <PJGarbageCollectionTest: 0x20000ee60> -init
2012-06-12 16:08:08.284 gcbridgetest[29712:903] Collecting with 2 external references
2012-06-12 16:08:08.285 gcbridgetest[29712:903] Collecting with 1 external reference
2012-06-12 16:08:08.286 gcbridgetest[29712:903] Collecting with 0 external references
2012-06-12 16:08:08.286 gcbridgetest[29712:903] <PJGarbageCollectionTest: 0x20000ee60> -finalize