NSAutoreleasePool 显示所有自动释放池的内容:
void test_showPools() {
NSObject *obj = [[[NSObject alloc] init] autorelease];
NSLog(@"obj:%@", obj);
SEL sel = NSSelectorFromString(@"showPools"); // 没有开放此接口,所以使用performSelector
// NSAutoreleasePool *pool = [NSAutoreleasePool new];
// [pool performSelector:sel]; // 会出现崩溃
[NSAutoreleasePool performSelector:sel]; // 能正确的输出
}
放在main.m(*此文件需要标识成禁止ARC)中
int main(int argc, char * argv[]) {
test_showPools();
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
输出的结果:
objc[1335]: ##############
objc[1335]: AUTORELEASE POOLS for thread 0x112c8e5c0
objc[1335]: 1 releases pending.
objc[1335]: [0x7ff276004000] ................ PAGE (hot) (cold)
objc[1335]: [0x7ff276004038] 0x600000018fc0 NSObject
objc[1335]: ##############
网友评论