NSMutableString 内存泄漏
我在代码中的仪器中遇到内存泄漏
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableString * res = [[[NSMutableString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]autorelease];
[webData release];
[connection release];
[res replaceOccurrencesOfString:@"&" withString:@"&" options:NSCaseInsensitiveSearch range:(NSRange){0,[res length]}];
[delegate getcat:res];
[pool drain];
}
- (void)getcat:(NSString*)xml
{
if (xmlParser) {
[xmlParser release];
}
Cid = [[NSMutableArray alloc] init];
Categories = [[NSMutableArray alloc] init];
NSData *data = [xml dataUsingEncoding:NSUTF8StringEncoding];
xmlParser = [[NSXMLParser alloc] initWithData:data];
[xmlParser setDelegate:self];
[xmlParser setShouldResolveExternalEntities:YES];
[xmlParser parse];
[xmlParser release];
}
这是管理内存的正确方法吗?
I am getting memory leak in instruments in the code
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableString * res = [[[NSMutableString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]autorelease];
[webData release];
[connection release];
[res replaceOccurrencesOfString:@"&" withString:@"&" options:NSCaseInsensitiveSearch range:(NSRange){0,[res length]}];
[delegate getcat:res];
[pool drain];
}
- (void)getcat:(NSString*)xml
{
if (xmlParser) {
[xmlParser release];
}
Cid = [[NSMutableArray alloc] init];
Categories = [[NSMutableArray alloc] init];
NSData *data = [xml dataUsingEncoding:NSUTF8StringEncoding];
xmlParser = [[NSXMLParser alloc] initWithData:data];
[xmlParser setDelegate:self];
[xmlParser setShouldResolveExternalEntities:YES];
[xmlParser parse];
[xmlParser release];
}
Is this the correct way to manage memory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与其设置一个自动释放池来实际释放字符串,为什么不自己释放它呢?如果委托保留了
getcat:
中的字符串,您可以简单地释放它:看看
getcat:
,我发现一个问题:通常,对象需要委托才能返回来自线程的结果。我假设 [xmlParser parse] 启动一个线程。您可能不应该在完成之前释放它,即您在
parserDidEndDocument:
中执行此操作。然而,这并不能解释许多泄露的字符串。
Instead of setting up an autorelease pool, which will actually release the string, why don't you just release it yourself? If the delegate retains the string in
getcat:
, you can simply release it:Taking a look at
getcat:
, I see a problem:Usually, objects need a delegate to return results from a thread. I assume that [xmlParser parse] starts a thread. You should probably not release it before it is finished, i.e. you do that in
parserDidEndDocument:
.This does however not explain the many leaked strings.
我修复了这个问题
泄漏位于
(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI QualifiedName:(NSString *)qName attribute:(NSDictionary *)attributeDict{
currentElement = [elementName copy];--->这一行总是泄漏,但泄漏仪器显示该行
}
I fixed this issue
The leak is in
(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
currentElement = [elementName copy];--->Always leaking in this line But leak instrument show that line
}