将字符串传递给对象时会泄漏字符串

时间:2011-05-12 13:44:43

标签: iphone objective-c ios memory-leaks instruments

我收到NSCFSting泄漏,可以追溯到下面的方法。我正在解析XML文件,并使用从textForElement获得的字符串并将它们设置为对象中的变量(EventArticleObject)。我知道泄漏不是来自textForElement,因为我没有在另一个解析方法中泄漏,我没有将字符串设置为对象。

我正在填充的数组和字典没有被泄露,它只是字符串。当我转到堆栈跟踪并单击其中一个泄漏字符串时,会显示以下内容: pic http://img820.imageshack.us/img820/9456/screenshot20110512at939.png #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

2 个答案:

答案 0 :(得分:1)

您是否使用link的{​​{1}}方法发布了-dealloc

答案 1 :(得分:1)

如果所有变量都正确发布,请验证EventArticleObject类