仪器报告因 NSURLConnection 的简单使用而泄漏:
#import <Foundation/Foundation.h>
int main (int argc, char ** argv)
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
NSURLRequest *theRequest = [NSURLRequest
requestWithURL:[NSURL URLWithString:@"https://gmail.com/"]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60.0];
[NSURLConnection connectionWithRequest:theRequest delegate:nil];
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:5]];
[pool drain];
[NSThread sleepForTimeInterval:10]; // wait for Instruments to check
}
泄漏堆栈跟踪:
0 CoreFoundation __CFBasicHashRehash
1 CoreFoundation CFDictionaryCreate
2 CFNetwork _getConnectionInfoForProxy
3 CFNetwork HTTPProtocol::createStream()
4 CFNetwork HTTPProtocol::createAndOpenStream()
5 CFNetwork executionContextPerform(void*)
6 CoreFoundation __CFRunLoopDoSources0
7 CoreFoundation __CFRunLoopRun
8 CoreFoundation CFRunLoopRunSpecific
9 CoreFoundation CFRunLoopRunInMode
10 Foundation +[NSURLConnection(NSURLConnectionReallyInternal) _resourceLoadLoop:]
11 Foundation -[NSThread main]
12 Foundation __NSThread__main__
13 libSystem.B.dylib _pthread_start
14 libSystem.B.dylib thread_start
如果 URL 只是普通的 http 并且不重定向到 https 站点,则不会泄漏。
如何修复泄漏?
我使用的是 Snow Leopard,并且我有 http 和 https 代理。
最佳答案
我认为你问了错误的问题。这就是您应该问的问题:
- 我是否遵守内存管理规则?
- 如果是的话,我需要担心这个吗?
就您而言,答案分别是"is"和“否”。您很可能在 Cocoa 框架中发现了内存泄漏。但在这种情况下,我认为你没有。您会看到,当前线程的 NSRunLoop
保留了 NSURLConnection
,因此您可能没有给运行循环足够的时间来释放连接或其他东西。
重点是,你没有做错任何事,所以不要 panic 。
关于objective-c - 使用代理时,NSURLConnection 会泄漏 https URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3114894/