在 Objective-C 中使用 TBXML 时出现内存泄漏

发布于 2024-12-07 10:19:27 字数 2176 浏览 1 评论 0原文

我是 Objective C 的新手,仍然不太清楚如何使用保留和释放。 在下面的代码中,我想使用 TBXML 解析 XML 文件并填充 TableView。该代码有效,但是当我“分析”我的应用程序时,Xcode 说变量 name 中存在内存泄漏。我想我应该在保留变量后释放它,但是,每当我尝试释放它时,无论我在哪里执行,它总是会产生错误。我也尝试不保留它,但它也产生了错误。

有人可以解释一下这里发生了什么吗?

- (void)loadNews {

    TBXML * tbxml = [[TBXML tbxmlWithURL:[NSURL URLWithString:@"http://www.abc/def.xml"]] retain];

    // If TBXML found a root node, process element and iterate all children
    if (tbxml.rootXMLElement) {

        TBXMLElement *categoryElement = [TBXML childElementNamed:@"category" parentElement:[tbxml rootXMLElement]];

        do {
            NSString *name = [TBXML valueOfAttributeNamed:@"name" forElement:categoryElement];
            [name retain]; // Something wrong with this line?

            NewsCategory *newsCategory = [[NewsCategory alloc] initWithCategoryName:name];

            // get entries in the category
            TBXMLElement *entryElement = [TBXML childElementNamed:@"entry" parentElement: categoryElement];

            do {
                NSString *title = [TBXML textForElement:[TBXML childElementNamed:@"title" parentElement:entryElement]];
                NSString * icon = [TBXML textForElement:[TBXML childElementNamed:@"icon" parentElement:entryElement]];
                NSString * link = [TBXML textForElement:[TBXML childElementNamed:@"link" parentElement:entryElement]];
                NSString * desc = [TBXML textForElement:[TBXML childElementNamed:@"desc" parentElement:entryElement]];

                NewsEntry *newsEntry = [[NewsEntry alloc] init];
                newsEntry.title = title;
                newsEntry.icon = icon;
                newsEntry.link = link;
                newsEntry.desc = desc;

                [newsCategory addEntry:newsEntry];

                [newsEntry release];
            } while((entryElement = entryElement->nextSibling));


            // save category
            [newsData addCategory:newsCategory];

            [newsCategory release];


        } while((categoryElement = categoryElement->nextSibling));


    }

    // release resources
    [tbxml release];


    [newsTableView reloadData];
}

I'm new to Objective C and am still not very clear about how to use retain and release.
In the following code, I want to use TBXML to parse an XML file and populate a TableView. The code works, but when I "Analyze" my app, Xcode says there are memory leaks in the variable name. I suppose I should release the variable after retaining it, however, whenever I tried to release it, no matter where I do it, it always produced an error. I also tried NOT to retain it, but it also produced an error.

Can somebody please explain what is happening here?

- (void)loadNews {

    TBXML * tbxml = [[TBXML tbxmlWithURL:[NSURL URLWithString:@"http://www.abc/def.xml"]] retain];

    // If TBXML found a root node, process element and iterate all children
    if (tbxml.rootXMLElement) {

        TBXMLElement *categoryElement = [TBXML childElementNamed:@"category" parentElement:[tbxml rootXMLElement]];

        do {
            NSString *name = [TBXML valueOfAttributeNamed:@"name" forElement:categoryElement];
            [name retain]; // Something wrong with this line?

            NewsCategory *newsCategory = [[NewsCategory alloc] initWithCategoryName:name];

            // get entries in the category
            TBXMLElement *entryElement = [TBXML childElementNamed:@"entry" parentElement: categoryElement];

            do {
                NSString *title = [TBXML textForElement:[TBXML childElementNamed:@"title" parentElement:entryElement]];
                NSString * icon = [TBXML textForElement:[TBXML childElementNamed:@"icon" parentElement:entryElement]];
                NSString * link = [TBXML textForElement:[TBXML childElementNamed:@"link" parentElement:entryElement]];
                NSString * desc = [TBXML textForElement:[TBXML childElementNamed:@"desc" parentElement:entryElement]];

                NewsEntry *newsEntry = [[NewsEntry alloc] init];
                newsEntry.title = title;
                newsEntry.icon = icon;
                newsEntry.link = link;
                newsEntry.desc = desc;

                [newsCategory addEntry:newsEntry];

                [newsEntry release];
            } while((entryElement = entryElement->nextSibling));


            // save category
            [newsData addCategory:newsCategory];

            [newsCategory release];


        } while((categoryElement = categoryElement->nextSibling));


    }

    // release resources
    [tbxml release];


    [newsTableView reloadData];
}

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

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

发布评论

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

评论(1

烟若柳尘 2024-12-14 10:19:27

如果创建 [TBXML valueOfAttributeNamed: forElement:] 的人遵循命名约定,则该值应该自动释放。您不需要保留它。

但是,您需要在[NewsCategory initWithCategoryName:]方法中保留或复制它。

If the guys who created [TBXML valueOfAttributeNamed: forElement:] followed the naming convention the value should be autoreleased. You do not need to retain it.

However, you need to retain or copy it in the [NewsCategory initWithCategoryName:] metod.

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