Jes*_*der 1 iphone xcode memory-leaks compilation
当我编译我的iPhone项目(在命令行中)时,日志输出如下所示:
2009-11-05 22:19:57.494 xcodebuild[51128:613] warning: compiler 'com.apple.compilers.llvm.clang.1_0.analyzer' is based on missing compiler 'com.apple.compilers.llvm.clang.1_0.analyzer'
=== BUILDING NATIVE TARGET Foo OF PROJECT foo WITH THE DEFAULT CONFIGURATION (AdHoc) ===
Checking Dependencies...
2009-11-05 22:19:58.032 xcodebuild[51128:5b07] *** _NSAutoreleaseNoPool(): Object 0x722d410 of class NSCFString autoreleased with no pool in place - just leaking
Stack: (0x97257f4f 0x97164432 0xfea624 0x2620e34 0x2620cbd 0x2384304 0x23957a3 0x948b76f0 0x948b8d35 0x948ae3c5 0x948aeaa8 0x2620922 0x9716adfd 0x9716a9a4 0x926bd155 0x926bd012)
2009-11-05 22:19:58.035 xcodebuild[51128:5b07] *** _NSAutoreleaseNoPool(): Object 0x720b370 of class NSCFDictionary autoreleased with no pool in place - just leaking
Stack: (0x97257f4f 0x97164432 0x2621454 0x262012a 0x2620e53 0x2620cbd 0x2384304 0x23957a3 0x948b76f0 0x948b8d35 0x948ae3c5 0x948aeaa8 0x2620922 0x9716adfd 0x9716a9a4 0x926bd155 0x926bd012)
2009-11-05 22:19:58.038 xcodebuild[51128:5b07] *** _NSAutoreleaseNoPool(): Object 0x720b370 of class NSCFDictionary autoreleased with no pool in place - just leaking
Stack: (0x97257f4f 0x97164432 0x2621454 0x2620174 0x2620e53 0x2620cbd 0x2384304 0x23957a3 0x948b76f0 0x948b8d35 0x948ae3c5 0x948aeaa8 0x2620922 0x9716adfd 0x9716a9a4 0x926bd155 0x926bd012)
2009-11-05 22:19:58.044 xcodebuild[51128:5b07] *** _NSAutoreleaseNoPool(): Object 0x7111690 of class NSCFDictionary autoreleased with no pool in place - just leaking
Stack: (0x97257f4f 0x97164432 0x2611215 0x2620f50 0x2620cbd 0x2384304 0x23957a3 0x948b76f0 0x948b8d35 0x948ae3c5 0x948aeaa8 0x2620922 0x9716adfd 0x9716a9a4 0x926bd155 0x926bd012)
2009-11-05 22:19:58.078 xcodebuild[51128:5b07] *** _NSAutoreleaseNoPool(): Object 0x7114c10 of class NSCFNumber autoreleased with no pool in place - just leaking
Stack: (0x97257f4f 0x97164432 0x2611242 0x2620f50 0x2620cbd 0x2384304 0x23957a3 0x948b76f0 0x948b8d35 0x948ae3c5 0x948aeaa8 0x2620922 0x9716adfd 0x9716a9a4 0x926bd155 0x926bd012)
2009-11-05 22:19:58.088 xcodebuild[51128:5b07] *** _NSAutoreleaseNoPool(): Object 0x71a28c0 of class NSCFNumber autoreleased with no pool in place - just leaking
Stack: (0x97257f4f 0x97164432 0x2611286 0x2620f50 0x2620cbd 0x2384304 0x23957a3 0x948b76f0 0x948b8d35 0x948ae3c5 0x948aeaa8 0x2620922 0x9716adfd 0x9716a9a4 0x926bd155 0x926bd012)
Run Code Online (Sandbox Code Playgroud)
然后有一大堆处理命令(一堆setenvs),然后CompileC命令开始,其中一些(但不是全部,可能是其中的10%)跟随更多关于泄漏的投诉.
但是,它正确编译,运行正常.我用Leaks运行它,它发现了两个16字节的泄漏,我还没有完全找到它,但没有任何关于上面消息的乱序.此外,它在编译时如何知道泄漏?
简而言之,编译器比你想象的更聪明;).这些错误意味着您在未分配NSAutoreleasePool的上下文中自动释放各种对象(NSString,NSDictionary,NSNumber).例如,项目的main.m文件应声明自动释放池:
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
Run Code Online (Sandbox Code Playgroud)
在许多应用程序中,这将是唯一分配的池.没有这个游泳池你就不能自动释放任何东西,如果你试图自动释放它没有游泳池去,所以它实际上是一个泄漏.检查您的主要功能,以确保您已分配NSAutoreleasePool.
在其他情况下,您可能需要声明另一个NSAutoreleasePool.最常见的情况是在新线程中调用选择器.每个线程都必须有自己的NSAutoreleasePool.有关这方面的更多信息,请参阅斯坦福大学CS193P第10讲的幻灯片36 :
- (void)someAction:(id)sender {
// Fire up a new thread
[NSThread detachNewThreadSelector:@selector(doWork:) withTarget:self object:someData];
}
- (void)doWork:(id)someData {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[someData doLotsOfWork];
// Message back to the main thread
[self performSelectorOnMainThread:@selector(allDone:) withObject:[someData result] waitUntilDone:NO];
[pool release];
}
Run Code Online (Sandbox Code Playgroud)
另一个例子是一个昂贵的循环,你自动释放许多对象.您不会让主要的NSAutoreleasePool变得庞大,而是会发现在循环中每N次创建一个新的NSAutoreleasePool将有助于将资源使用降至最低.其中一些代码也是从上面列出的演讲幻灯片中借用的:
int N = 10;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
for (int i = 0; i < someLargeNumber; i++) {
NSString *string = ...;
string = [string lowercaseString];
string = [string stringByAppendingString:...];
NSLog(@“%@”, string);
// Release the temporary pool and allocate a new one
// every N times around the loop
if (i % N == 0) {
[pool release];
pool = [[NSAutoreleasePool alloc] init];
}
}
[pool release];
Run Code Online (Sandbox Code Playgroud)
也许您有一些像这样的代码并不总是按预期分配或释放池.我不确定编译器是否足够聪明,可以注意到这类问题.
| 归档时间: |
|
| 查看次数: |
1212 次 |
| 最近记录: |