开发者

Leaking a string when giving it to object

I am receiving NSCFSting leaks that are traced back to the method below. I am parsing an XML file, and using the strings obtained from textForElement and setting them to variables in an object (EventArticleObject). I know the leaks are not coming from textForElement, as I have no leaks in another parsing method w开发者_JAVA技巧here I am not setting the strings to an object.

The array and dictionary I am filling are not being leaked, it is just the strings. When I goto the Stack Trace and click on one of the leaking strings, this is what shows up:

Leaking a string when giving it to object

The #2 CFRetain is traced back to setLink in the method below, that is what I believe that I am doing something wrong when using objects.

Does anyone see anything I am doing wrong here? Thanks a lot!

//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];
        }
    }
}
}

Here is my 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


Verify the EventArticleObject class if all the variables are released properly


Do you release link in the -dealloc method of EventArticleObject?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜