est*_*art 9 xcode cocoa memory-management objective-c
如果没有自动释放池,我正在尝试捕获方案.
这是我的测试应用.
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[self performSelectorInBackground:@selector(bgOperation:) withObject:nil];
}
- (void)bgOperation:(id)obj
{
NSString *string [[[NSString alloc] init] autorelease];
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试设置断点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);
}
Run Code Online (Sandbox Code Playgroud)
而这..
在我的appDelegate中,也没有结果.
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[NSThread detachNewThreadSelector:@selector(bgOperation:)
toTarget:self
withObject:nil];
}
Run Code Online (Sandbox Code Playgroud)
而这......
在我的主题中使用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);
}
Run Code Online (Sandbox Code Playgroud)
据我所知,由于操作系统级别可能没有泄漏(仍未确认),但当我以10.6为目标时,我在日志中看到了一些"无池"消息.如果它只是由于操作系统级别而泄漏,那么当我以10.6为目标但使用10.7 SDK时,我需要一种方法来捕获10.7中的这些场景.
performSelectorInBackground:现在可能正在使用dispatch_queue,它会自动为您设置自动释放池.尝试直接启动新的NSThread并查看是否会导致泄漏.
您也可以尝试将代码移动到NSApplicationMain之前,这将具有相同的行为.
| 归档时间: |
|
| 查看次数: |
243 次 |
| 最近记录: |