gpt4 book ai didi

iphone - 使用 TBXML textFromElement 时发生内存泄漏

转载 作者:行者123 更新时间:2023-11-28 17:45:55 24 4
gpt4 key购买 nike

我使用 TBXML 的方法收到内存泄漏解析 XML 文件。

它给我 NSCFString 'Leaked Objects' with。当我查看堆栈跟踪时,它的最后一次调用是使用 TBXML 方法 textForElement;用于获取元素中的字符串。

如果我单击跟踪,它会将我带到 TBXML 类文件中的此方法:

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

这是使用 textForElement 的方法之一:

- (NSMutableArray *)parseNewsXML
{

NSString *newsURL = @"http://www.millersville.edu/news/rss.php";

NSMutableArray *newsArray = [[NSMutableArray alloc] init];

EventArticleObject *currentArticle = [[EventArticleObject alloc] init];


tbxml = [TBXML tbxmlWithURL:[NSURL URLWithString:newsURL]];
rootXMLElement = tbxml.rootXMLElement;

if(rootXMLElement)
{
node_channel = [TBXML childElementNamed:@"channel" parentElement:rootXMLElement];

if(node_channel)
{
node_item = [TBXML childElementNamed:@"item" parentElement:node_channel];

while(node_item)
{
node_traverse = [TBXML childElementNamed:@"title" parentElement:node_item];
NSString *title = [TBXML textForElement:node_traverse];
title = [title stringByReplacingOccurrencesOfString:@""" withString:@"\""];


[currentArticle setTitle:title];
node_traverse = [TBXML childElementNamed:@"link" parentElement:node_item];
NSString *link = [TBXML textForElement:node_traverse];
[currentArticle setLink:link];
[currentArticle setDate:nil];

[newsArray addObject:currentArticle];
node_item = node_item -> nextSibling;
}
}
}
[currentArticle release];
return newsArray;
}

这里是第二种方法也收到相同的泄漏:

- (void)parseMuAlertXML
{

time_t unixTime = (time_t) [[NSDate date] timeIntervalSince1970];

NSURL *url = [NSURL URLWithString:@"http://www.millersville.edu/alert/alert.xml"];

tbxml = [TBXML tbxmlWithURL:url];
rootXMLElement = tbxml.rootXMLElement;

if(rootXMLElement)
{
node_alert = [TBXML childElementNamed:@"alert" parentElement:rootXMLElement];

if(node_alert)
{
node_traverse = [TBXML childElementNamed:@"startdate" parentElement:node_alert];
NSInteger startString = [[TBXML textForElement:node_traverse] intValue];
time_t startTime = startString;

node_traverse = [TBXML childElementNamed:@"enddate" parentElement:node_alert];
NSInteger endString = [[TBXML textForElement:node_traverse] intValue];
time_t endTime = endString;

node_traverse = [TBXML childElementNamed:@"type" parentElement:node_alert];
alertType = [[TBXML textForElement:node_traverse] retain];

node_message = [TBXML childElementNamed:@"message" parentElement:node_alert];

if(node_message)
{
node_traverse = [TBXML childElementNamed:@"p" parentElement:node_message];
alertMessage = [TBXML textForElement:node_traverse];
alertMessage = [[alertMessage stringByReplacingOccurrencesOfString:@" " withString:@" "] retain];
}

if((unixTime >= startTime) && (unixTime <= endTime))
{
[alertButton setHidden:NO];
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
} else {
[alertButton setHidden:YES];
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
}
}
}
}

有人看到那里出了什么问题吗?谢谢。

最佳答案

您分配了 newsArray 并在函数中返回了它。您稍后会发布它吗?您正在使用它吗?

该工具似乎不理解这一点。它认为您分配了并且没有释放它。

理想情况下,您应该自动释放需要从函数返回的对象。

关于iphone - 使用 TBXML textFromElement 时发生内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5953835/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com