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

发布于 2024-11-06 13:14:02 字数 3596 浏览 1 评论 0原文

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

我填充的数组和字典没有被泄漏,它只是字符串。当我转到堆栈跟踪并单击泄漏字符串之一时,将显示以下内容: pic#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

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 where 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:
pic

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

活泼老夫 2024-11-13 13:14:02

验证 EventArticleObject 类是否所有变量都已正确释放

Verify the EventArticleObject class if all the variables are released properly

注定孤独终老 2024-11-13 13:14:02

你是否在EventArticleObject-dealloc方法中释放了link

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文