我收到的NSCFSting泄漏可追溯到以下方法。我正在解析XML文件,并使用从textForElement获得的字符串并将它们设置为对象(EventArticleObject)中的变量。我知道泄漏不是来自textForElement,因为我在另一个没有将字符串设置为对象的解析方法中也没有泄漏。
我要填充的数组和字典没有泄漏,而只是字符串。当我转到堆栈跟踪并单击泄漏的字符串之一时,将显示以下内容:
#2 CFRetain可以追溯到以下方法中的setLink,这就是我认为使用对象时做错了什么。
有人在这里看到我做错了什么吗?非常感谢!
//Parse the weekly events are store. Must first get the date, format it, and attach it to
// the link; this is to get the events for the current week.
- (void)parseWeekEvents
{
TBXML *tbxml;
TBXMLElement *rootXMLElement;
TBXMLElement *node_channel;
TBXMLElement *node_item;
TBXMLElement *node_traverse;
NSString *fullEventURL;
fullEventURL = @"http://www.millersville.edu/calendar/rss.php?q=&c=&date=";
fullEventURL = [fullEventURL stringByAppendingString:dateURL];
fullEventURL = [fullEventURL stringByAppendingString:@"&mode=week"];
eventsDict = [[NSMutableDictionary alloc] init];
datesArray = [[NSMutableArray alloc] init];
tbxml = [TBXML tbxmlWithURL:[NSURL URLWithString:fullEventURL]];
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)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
EventArticleObject *currentEvent = [[[EventArticleObject alloc] init] autorelease];
NSString *title;
NSString *link;
NSString *date;
node_traverse = [TBXML childElementNamed:@"title" parentElement:node_item];
title = [TBXML textForElement:node_traverse];
title = [title stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
title = [title stringByReplacingOccurrencesOfString:@"'" withString:@"'"];
[currentEvent setTitle:title];
node_traverse = [TBXML childElementNamed:@"link" parentElement:node_item];
link = [TBXML textForElement:node_traverse];
[currentEvent setLink:link];
node_traverse = [TBXML childElementNamed:@"pubDate" parentElement:node_item];
date = [TBXML textForElement:node_traverse];
NSRange stringRange = {0,16};
date = [date substringWithRange:stringRange];
[currentEvent setDate:date];
if(![datesArray containsObject:date])
{
[datesArray addObject:date];
}
NSString *eventDate = [currentEvent date];
NSMutableArray *temp = [eventsDict objectForKey:eventDate];
if(!temp)
{
temp = [NSMutableArray array];
[temp addObject:currentEvent];
[eventsDict setObject:temp forKey:eventDate];
} else {
[temp addObject:currentEvent];
}
node_item = node_item -> nextSibling;
[pool drain];
}
}
}
}
这是我的EventArticleObject.m:
@implementation EventArticleObject
@synthesize link, date, title;
- (id)initWithTitle:(NSString *)title2
date:(NSString *)date2
link:(NSString *)link2;
{
self = [super init];
if(!self)
return nil;
[self setLink:link2];
[self setDate:date2];
[self setTitle:title2];
return self;
}
@end
最佳答案
如果所有变量均已正确释放,请验证EventArticleObject类
关于iphone - 将字符串提供给对象时泄漏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5978943/