在 Objective-C 中使用 TBXML 时出现内存泄漏
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果创建 [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.