使用Xcode为iPhone*编译*时的内存泄漏日志消息
编译iPhone项目时(在命令行中),日志输出如下所示:使用Xcode为iPhone*编译*时的内存泄漏日志消息,iphone,xcode,memory-leaks,compilation,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 PR
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)
然后有一大堆处理命令(一堆setenv
s),然后CompileC
命令启动,其中一些(但不是全部,可能是10%)随后会有更多关于泄漏的投诉
但是,它编译正确,运行良好。我用漏洞运行了它,它发现了两个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;
}
在许多应用程序中,这将是唯一分配的池。没有这个池,你不能自动释放任何东西,如果你尝试自动释放,它没有池去,所以它实际上成为一个泄漏。检查主功能,以确保已分配NSAutoreleasePool
在其他情况下,您可能需要声明另一个NSAutoreleasePool。最常见的情况是在新线程中调用选择器。每个线程都必须有自己的NSAutoreleasePool。有关这方面的更多信息,请参见第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];
}
另一个例子是一个昂贵的循环,在这个循环中,您将自动释放许多对象。与其让主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];
int N=10;
NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init];
for(int i=0;i
也许您有一些这样的代码,它们并不总是按照预期分配或释放池。不过,我不确定编译器是否足够聪明,是否会注意到此类问题。简而言之,编译器比您想象的更聪明;)。这些错误意味着您正在未分配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;
}
在许多应用程序中,这将是唯一分配的池。没有这个池,你不能自动释放任何东西,如果你尝试自动释放,它没有池去,所以它实际上成为一个泄漏。检查主功能,以确保已分配NSAutoreleasePool
在其他情况下,您可能需要声明另一个NSAutoreleasePool。最常见的情况是在新线程中调用选择器。每个线程都必须有自己的NSAutoreleasePool。有关这方面的更多信息,请参见第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];
}
另一个例子是一个昂贵的循环,在这个循环中,您将自动释放许多对象。与其让主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];
int N=10;
NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init];
for(int i=0;i
也许您有一些这样的代码,它们并不总是按照预期分配或释放池。不过,我不确定编译器是否足够聪明,能够注意到这种类型的问题。在我的
UIApplicationMain
之前,我确实分配了一个自动释放池;然而,它在一个已经编译过的文件中,我只是通过这个项目链接到它。那么,也许编译器太聪明了,不利于自己?嗯,这似乎就是问题所在。我将自动释放池移回项目中,警告消失了。我想这可能是Xcode本身的问题,我从来没有尝试过从命令行编译,所以我以前没有见过这种情况。你刚刚把文件添加到你的p中了吗