简体   繁体   English

NSMutableString内存泄漏

[英]NSMutableString memory leak

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? 这是管理内存的正确方法吗?

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: 如果委托将字符串保留在getcat: ,则可以简单地释放它:

- (void) connectionDidFinishLoading: (NSURLConnection *) connection
{
    // Omit the autorelease pool.

    NSMutableString * res = [[NSMutableString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
    [webData release];
    [connection release];
    [res replaceOccurrencesOfString:@"&" withString:@"&" options:NSCaseInsensitiveSearch range:NSMakeRange(0, res.length)];
    [delegate getcat:res];
    [res release];
}

Taking a look at getcat: , I see a problem: 看一下getcat:我看到一个问题:

[xmlParser parse];
[xmlParser release];

Usually, objects need a delegate to return results from a thread. 通常,对象需要委托才能从线程返回结果。 I assume that [xmlParser parse] starts a thread. 我假设[xmlParser parse]启动了一个线程。 You should probably not release it before it is finished, ie you do that in parserDidEndDocument: . 您可能不应该在完成之前释放它,即您可以在parserDidEndDocument:中进行parserDidEndDocument:

This does however not explain the many leaked strings. 但是,这不能解释许多泄漏的字符串。

I fixed this issue 我解决了这个问题

The leak is in 泄漏在

  • (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURIqualifiedName:(NSString *)qName属性:(NSDictionary *)attributeDict {

    currentElement = [elementName copy]; currentElement = [elementName复制]; --->Always leaking in this line But leak instrument show that line --->此行始终泄漏,但泄漏仪器显示该行

} }

replace the code with self.currentElemnt=elementName self.currentElemnt = elementName替换代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM