English 中文(简体)
NSScanner memory leak
原标题:

I m at my first experiences with iPhone development. I wrote some basic code to test the NSScanner class, and now I was looking into the Leaks tool. It seems that this code is leaking, when in the detailed stack I double-click my last call (before Apple s stuff), the incriminated line is the commented.

Can anyone help me to understand why this is leaking? from a logical point of view the result is what I expect, and I do not formally alloc anything myself (except for the xmlblock variable, which is btw autoreleased), so I would not expect the need to release anything... where I m wrong? :-)

+(NSSet *)extractXMLSectionsWithTag:(NSString *)tag fromString:(NSString *)source firstOnly:(BOOL)firstOnly
{
 if (!source)
    return nil;
 NSScanner *scanner = [NSScanner scannerWithString:source];
 NSString *openingToken = [NSString stringWithFormat:@"<%@", tag];
 NSString *closingToken = [NSString stringWithFormat:@"</%@>", tag];
 NSMutableSet *sections = [NSMutableSet set];
 NSCharacterSet *majorChar = [NSCharacterSet characterSetWithCharactersInString:@">"];

 while (![scanner isAtEnd]) {
    NSString *xmlBlock = [[[NSString alloc] init] autorelease];
    [scanner scanUpToString:openingToken intoString:NULL];
    [scanner scanString:openingToken intoString:NULL];
    [scanner scanUpToCharactersFromSet:majorChar intoString:NULL];
    [scanner scanCharactersFromSet:majorChar intoString:NULL];
    [scanner scanUpToString:closingToken intoString:&xmlBlock];
    if (![xmlBlock isEqualToString:@""]) { // Leaking line
        [sections addObject:xmlBlock];
        if (firstOnly) {
            break;
        }
    }
 }
 return [sections copy];
}
最佳回答

I m pretty sure the leak is caused by returning [sections copy], from a method like that you are supposed to return an autoreleased string - not a retained one.

In XCode choose the "Build & Analyze" option and see what it says about this code.

问题回答

暂无回答




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签