NSMutableString 内存泄漏

发布于 2024-12-02 01:49:24 字数 1071 浏览 5 评论 0原文

我在代码中的仪器中遇到内存泄漏在此处输入图像描述

-(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 codeenter image description here

-(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆依然 2024-12-09 01:49:24

与其设置一个自动释放池来实际释放字符串,为什么不自己释放它呢?如果委托保留了 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];
}

看看 getcat:,我发现一个问题:

[xmlParser parse];
[xmlParser release];

通常,对象需要委托才能返回来自线程的结果。我假设 [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:

- (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:

[xmlParser parse];
[xmlParser release];

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.

纸伞微斜 2024-12-09 01:49:24

我修复了这个问题

泄漏位于

  • (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI QualifiedName:(NSString *)qName attribute:(NSDictionary *)attributeDict{

    currentElement = [elementName copy];--->这一行总是泄漏,但泄漏仪器显示该行

}

将代码替换为self.currentElemnt=elementName

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

}

replace the code with self.currentElemnt=elementName

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文