开发者

Major leak in my NSDateFormatter script

I used Instrument to check if I had any leaks in my script and it came with a couple particularly in my NSDateFormatter. It says I had it:

Leaked Object    #   Address         Size       Responsible Library   Responsible Frame
NSDateFormatter  70  < multiple >   1.09 KB    DAF                   +[XMLParser dateFromString:]

This is my method it points at and I cannot find any leak:

+ (NSDate *)dateFromString:(NSString *)dateString
{
    NSDateFormatter *nsDateFormatter = [[NSDateFormatter alloc] init];
    [nsDateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm"];
    NSDate *date = [nsDateFormatter dateFromString:dateStrin开发者_JAVA技巧g];
    return date;
    [nsDateFormatter release];
}

Can anybody help me with this one? I have no idea where to look this is my first time with Instruments.


You are returning date before releasing the formatter.

+ (NSDate *)dateFromString:(NSString *)dateString
{
    NSDateFormatter *nsDateFormatter = [[NSDateFormatter alloc] init];
    [nsDateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm"];
    NSDate *date = [nsDateFormatter dateFromString:dateString];
    [nsDateFormatter release]; //Release here
    return date;
    //Code after a return does not get executed!!!
}


Make your return statement the last statement. As you have written it, [nsDateFormatter release] is never called, because the function is returning before it can execute that line.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜