iPhone自动释放字符串导致内存泄漏

发布于 2024-09-09 00:00:48 字数 1489 浏览 2 评论 0原文

我正在尝试使用泄漏工具清除我的应用程序的泄漏。 它向我展示了 xml 解析器 (TBXML) 上的泄漏。

这是我将在解析时创建的一个类:

@interface GraphPoint : NSObject {
    NSString* x;
    NSString* y;
}


@property (nonatomic, copy) NSString* x;
@property (nonatomic, copy) NSString* y;

@end

@implementation GraphPoint

@synthesize x, y;

... some calculations

- (void) dealloc
{
    [x release];
    [y release];
    [super dealloc];
}

@end

在解析器中:

... // 当根据元素找到时:

        NSString    *str;
        GraphPoint  *aPoint = [[GraphPoint alloc] init];

        TBXMLElement *item = [TBXML childElementNamed:kX_Item parentElement:pntItem];
        str = [TBXML textForElement:item];  
        aPoint.x = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];     

        item = [TBXML childElementNamed:kY_Item parentElement:pntItem];
        str = [TBXML textForElement:item];  
        aPoint.y = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];


        [points addObject:aPoint];
        [aPoint release];

泄漏工具在 TBXML 的 textForElement 函数中显示泄漏,该函数提供自动释放的字符串:

+ (NSString*) textForElement:(TBXMLElement*)aXMLElement {
    if (nil == aXMLElement->text) return @"";
    return [NSString stringWithCString:&aXMLElement->text[0] encoding:NSUTF8StringEncoding];
}

由于我们有时谈论数百甚至数千个点,因此这些泄漏变得非常巨大。我不明白为什么自动释放的字符串会产生泄漏?

有什么想法吗?

谢谢

I'm trying to clean my app from leaks with Leak instrument.
It shows me leaks on xml parser (TBXML).

Here is a class I'm going to create upon the parsing:

@interface GraphPoint : NSObject {
    NSString* x;
    NSString* y;
}


@property (nonatomic, copy) NSString* x;
@property (nonatomic, copy) NSString* y;

@end

@implementation GraphPoint

@synthesize x, y;

... some calculations

- (void) dealloc
{
    [x release];
    [y release];
    [super dealloc];
}

@end

In the parser:

...
// When found according element:

        NSString    *str;
        GraphPoint  *aPoint = [[GraphPoint alloc] init];

        TBXMLElement *item = [TBXML childElementNamed:kX_Item parentElement:pntItem];
        str = [TBXML textForElement:item];  
        aPoint.x = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];     

        item = [TBXML childElementNamed:kY_Item parentElement:pntItem];
        str = [TBXML textForElement:item];  
        aPoint.y = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];


        [points addObject:aPoint];
        [aPoint release];

Leaks instrument shows leak in TBXML's textForElement function, which provides autoreleased string:

+ (NSString*) textForElement:(TBXMLElement*)aXMLElement {
    if (nil == aXMLElement->text) return @"";
    return [NSString stringWithCString:&aXMLElement->text[0] encoding:NSUTF8StringEncoding];
}

Since we're talking sometimes about hundreds or even thousands of points, these leaks become something huge. I cannot understand why autoreleased string produce leaks?

Any thoughts?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

夏雨凉 2024-09-16 00:00:48

发布的代码中不存在保留/释放问题。 textForElement: 中唯一的分配是 NSString 的 stringWithCString:encoding:,我怀疑它会泄漏。

问题在其他地方,无法用给定的信息来回答。您确定您正确读取仪器结果吗?静态分析对代码有何影响?

我不了解 TBXML 库,但是包含 nil == aXMLElement->text 的行使它看起来有点可疑。这不是一个错误,而是一个风格问题:aXMLElement->text 似乎是一个 C 字符串,而 nil 用于 objc 对象。

There are no retain/release problems in the posted code. The only allocation in textForElement: is NSString's stringWithCString:encoding:, which I doubt to leak.

The problem is elsewhere and cannot be answered with the given information. Are you sure that you are reading Instruments results correctly? What does static analysis say about the code?

I don't know about the TBXML library, but the line containing nil == aXMLElement->text makes it look a bit fishy. It's not an error but a question of style: aXMLElement->text seems to be a C string while nil is used for objc objects.

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