【发布时间】:2012-12-19 16:17:42
【问题描述】:
如果没有自动释放池,我会尝试捕捉场景。
这是我的测试应用。
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[self performSelectorInBackground:@selector(bgOperation:) withObject:nil];
}
- (void)bgOperation:(id)obj
{
NSString *string [[[NSString alloc] init] autorelease];
}
我已尝试设置断点 objc_autoreleaseNoPool。
我已经尝试使用 Instruments / Leaks 进行分析。
OSX 10.7.5 XCode 4.3.3 以 10.6 为目标,AutomaticRefCounting = NO,GarbageCollection = 不支持。
我了解 NSApplication 包含它自己的自动释放池。但我的理解是每次调用 performSelectorInBackground: 都需要它自己的自动释放池。
建议更新:
我试过这个..
在 main.m 中,没有运气。
int main(int argc, char *argv[])
{
NSString *junk = [[[NSString alloc]init]autorelease];
return NSApplicationMain(argc, (const char **)argv);
}
还有这个..
在我的 appDelegate 中,也没有结果。
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[NSThread detachNewThreadSelector:@selector(bgOperation:)
toTarget:self
withObject:nil];
}
还有这个..
在我的 main.m 中使用 pthreads
void *doJunk(void *ptr){
NSString *junk = [[[NSString alloc]initWithString:@"string with no pool"]autorelease];
NSLog(@"%@", junk);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
pthread_t thread;
pthread_create(&thread, NULL, doJunk, NULL);
return NSApplicationMain(argc, (const char **)argv);
}
我了解由于操作系统级别的原因,可能没有任何泄漏(仍未确认),但是当我以 10.6 为目标时,我在日志中看到许多“无池”消息。如果它只是由于操作系统级别而泄漏,那么当我的目标是 10.6 但使用 10.7 SDK 时,我需要一种方法在 10.7 中捕获这些场景。
【问题讨论】:
-
您是否在日志中查找过“刚刚泄漏”的消息?
-
只有在运行应用程序并在 10.6 中观看控制台时才能看到它们。
标签: objective-c xcode cocoa memory-management