仪器(XCode4)报告ASIHttpRequest泄漏内存?
我在 XCode 4 中得到 Instrument 工具报告来自 ASIHttpRequest 的内存泄漏...我无法找出问题,转而我注释掉了所有代码来处理结果并使函数如下所示,但 xcode 仍然报告同样的内存泄漏...
每次单击按钮时都会调用此方法,每次单击按钮时我都会看到更多的内存泄漏发生。 :(
- (void) loadData
{
// no data set, we need to load ourself
NSURL *url = [self getDataUrl];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
////////////////////////////////////////////////////////////////////
// set cache policy
//
// always store data in cache
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
// Always ask the server if there is new content available,
// If the request fails, use data from the cache even if it should have expired.
[request setCachePolicy:ASIAskServerIfModifiedWhenStaleCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy];
[request setCompletionBlock:^{
NSLog(@"[http request] finishing %@", url);
[self dataDidLoadSuccess];
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(@"[http request]Failed to perform request to %@: %@", url, error);
[self dataDidLoadFail:error];
}];
[request startAsynchronous];
}
以下内容复制自仪器检测到的泄漏(仅部分):
Leaked Object # Address Size Responsible Library Responsible Frame
__NSMallocBlock__,2 < multiple > 64 Bytes UIKit -[UIViewController view]
NSCFString,2 < multiple > 64 Bytes CFNetwork HTTPMessage::parseHeadersFromData()
GeneralBlock-16,2 < multiple > 32 Bytes Foundation -[NSThread main]
NSRecursiveLock,2 < multiple > 160 Bytes Foundation +[NSRecursiveLock allocWithZone:]
NSConcreteMutableData,2 < multiple > 64 Bytes Foundation +[NSMutableData(NSMutableData) allocWithZone:]
__NSArrayM,2 < multiple > 64 Bytes UIKit -[UIViewController view]
__NSMallocBlock__,2 < multiple > 64 Bytes UIKit -[UIViewController view]
__NSArrayM,2 < multiple > 64 Bytes Foundation +[NSHTTPCookie _cf2nsCookies:]
__NSOperationInternal,2 < multiple > 288 Bytes Foundation -[NSOperation init]
NSCFString, 0xb35fdc0 16 Bytes CFNetwork createCapitalizedHeaderString
NSCFString, 0xb35fda0 32 Bytes CFNetwork HTTPMessage::extractResponseStatusLine(unsigned char const*, long)
GeneralBlock-32, 0xb35cd10 32 Bytes CFNetwork HTTPMessage::internalSetHeader(__CFString const*, __CFString const*, long)
__NSCFArray, 0xb35c550 32 Bytes CFNetwork HTTPReadStream::streamEvent(unsigned long)
GeneralBlock-48, 0xb35c520 48 Bytes CFNetwork HTTPReadStream::startRequest(CFStreamError*)
GeneralBlock-16, 0xb35c440 16 Bytes CFNetwork HTTPReadStream::startRequest(CFStreamError*)
__NSCFInputStream, 0xb35c420 32 Bytes CFNetwork HTTPReadStream::startRequest(CFStreamError*)
GeneralBlock-32, 0xb35ba80 32 Bytes CFNetwork HTTPReadStream::constructProxyList(CFStreamError*)
__NSCFArray, 0xb35ba60 32 Bytes CFNetwork HTTPReadStream::constructProxyList(CFStreamError*)
GeneralBlock-48, 0xb35ba10 48 Bytes CFNetwork HTTPMessage::initialize(HTTPMessage*)
CFHTTPMessage, 0xb35b950 80 Bytes CFNetwork HTTPReadStream::streamOpen(__CFReadStream*, CFStreamError*, unsigned char*)
GeneralBlock-48, 0xb35b920 48 Bytes Foundation -[NSThread main]
__NSCFArray, 0xb35b900 32 Bytes CFNetwork HTTPMessage::initialize(HTTPMessage*)
__NSCFArray, 0xb35b8e0 32 Bytes Foundation -[NSThread main]
__NSCFArray, 0xb35b8c0 32 Bytes CFNetwork HTTPReadStream::startRequest(CFStreamError*)
GeneralBlock-48, 0xb35b610 48 Bytes Foundation -[NSThread main]
GeneralBlock-16, 0xb35b5f0 16 Bytes CFNetwork HTTPReadStream::streamSetProperty(__CFReadStream*, __CFString const*, void const*)
GeneralBlock-32, 0xb35b5d0 32 Bytes Foundation -[NSThread main]
GeneralBlock-32, 0xb35b5b0 32 Bytes Foundation -[NSThread main]
NSCFString, 0xb35b590 32 Bytes Foundation -[NSURL(NSURL) host]
GeneralBlock-16, 0xb35b570 16 Bytes CFNetwork HTTPReadStream::streamSetProperty(__CFReadStream*, __CFString const*, void const*)
__NSCFDictionary, 0xb35b540 48 Bytes Foundation -[NSThread main]
__NSCFDictionary, 0xb35b490 48 Bytes CFNetwork
I get Instrument tools in XCode 4 report memory leaks from ASIHttpRequest... I didn't be able to figure out the problem, turned our I commented out all my code to handle the result and make the function like below, but xcode still report the same memory leak...
This method is called everytime when I click a button, and I will see more memory leak happen every time when I hit the button. :(
- (void) loadData
{
// no data set, we need to load ourself
NSURL *url = [self getDataUrl];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
////////////////////////////////////////////////////////////////////
// set cache policy
//
// always store data in cache
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
// Always ask the server if there is new content available,
// If the request fails, use data from the cache even if it should have expired.
[request setCachePolicy:ASIAskServerIfModifiedWhenStaleCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy];
[request setCompletionBlock:^{
NSLog(@"[http request] finishing %@", url);
[self dataDidLoadSuccess];
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(@"[http request]Failed to perform request to %@: %@", url, error);
[self dataDidLoadFail:error];
}];
[request startAsynchronous];
}
Following is copied from Instrument detected leaks (only a part):
Leaked Object # Address Size Responsible Library Responsible Frame
__NSMallocBlock__,2 < multiple > 64 Bytes UIKit -[UIViewController view]
NSCFString,2 < multiple > 64 Bytes CFNetwork HTTPMessage::parseHeadersFromData()
GeneralBlock-16,2 < multiple > 32 Bytes Foundation -[NSThread main]
NSRecursiveLock,2 < multiple > 160 Bytes Foundation +[NSRecursiveLock allocWithZone:]
NSConcreteMutableData,2 < multiple > 64 Bytes Foundation +[NSMutableData(NSMutableData) allocWithZone:]
__NSArrayM,2 < multiple > 64 Bytes UIKit -[UIViewController view]
__NSMallocBlock__,2 < multiple > 64 Bytes UIKit -[UIViewController view]
__NSArrayM,2 < multiple > 64 Bytes Foundation +[NSHTTPCookie _cf2nsCookies:]
__NSOperationInternal,2 < multiple > 288 Bytes Foundation -[NSOperation init]
NSCFString, 0xb35fdc0 16 Bytes CFNetwork createCapitalizedHeaderString
NSCFString, 0xb35fda0 32 Bytes CFNetwork HTTPMessage::extractResponseStatusLine(unsigned char const*, long)
GeneralBlock-32, 0xb35cd10 32 Bytes CFNetwork HTTPMessage::internalSetHeader(__CFString const*, __CFString const*, long)
__NSCFArray, 0xb35c550 32 Bytes CFNetwork HTTPReadStream::streamEvent(unsigned long)
GeneralBlock-48, 0xb35c520 48 Bytes CFNetwork HTTPReadStream::startRequest(CFStreamError*)
GeneralBlock-16, 0xb35c440 16 Bytes CFNetwork HTTPReadStream::startRequest(CFStreamError*)
__NSCFInputStream, 0xb35c420 32 Bytes CFNetwork HTTPReadStream::startRequest(CFStreamError*)
GeneralBlock-32, 0xb35ba80 32 Bytes CFNetwork HTTPReadStream::constructProxyList(CFStreamError*)
__NSCFArray, 0xb35ba60 32 Bytes CFNetwork HTTPReadStream::constructProxyList(CFStreamError*)
GeneralBlock-48, 0xb35ba10 48 Bytes CFNetwork HTTPMessage::initialize(HTTPMessage*)
CFHTTPMessage, 0xb35b950 80 Bytes CFNetwork HTTPReadStream::streamOpen(__CFReadStream*, CFStreamError*, unsigned char*)
GeneralBlock-48, 0xb35b920 48 Bytes Foundation -[NSThread main]
__NSCFArray, 0xb35b900 32 Bytes CFNetwork HTTPMessage::initialize(HTTPMessage*)
__NSCFArray, 0xb35b8e0 32 Bytes Foundation -[NSThread main]
__NSCFArray, 0xb35b8c0 32 Bytes CFNetwork HTTPReadStream::startRequest(CFStreamError*)
GeneralBlock-48, 0xb35b610 48 Bytes Foundation -[NSThread main]
GeneralBlock-16, 0xb35b5f0 16 Bytes CFNetwork HTTPReadStream::streamSetProperty(__CFReadStream*, __CFString const*, void const*)
GeneralBlock-32, 0xb35b5d0 32 Bytes Foundation -[NSThread main]
GeneralBlock-32, 0xb35b5b0 32 Bytes Foundation -[NSThread main]
NSCFString, 0xb35b590 32 Bytes Foundation -[NSURL(NSURL) host]
GeneralBlock-16, 0xb35b570 16 Bytes CFNetwork HTTPReadStream::streamSetProperty(__CFReadStream*, __CFString const*, void const*)
__NSCFDictionary, 0xb35b540 48 Bytes Foundation -[NSThread main]
__NSCFDictionary, 0xb35b490 48 Bytes CFNetwork
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定这是否是您遇到的问题,但引用 http://allseeing-i.com/ASIHTTPRequest/How-to-use#using_blocks:
因此,尝试添加 __block 限定符,然后重新测试,看看问题是否仍然存在......
I'm not sure if this is the problem you're having or not, but to quote http://allseeing-i.com/ASIHTTPRequest/How-to-use#using_blocks :
So try adding the __block qualifier, and retest and see if you still have the problem...