tmdvs / TDBadgedCell

TDBadgedCell is a table view cell class that adds a badge, similar to the badges in Apple's own apps

Home Page:www.tmdvs.me

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Memory leak on non ARC environment

yoasha opened this issue · comments

When using Xcode 5 analyzer, I get the following warning:

...TDBadgedCell.m:99:5: Potential leak of an object stored into 'paragraph'

There is also actual memory leak when profiling using Instruments!

This is the current code:

#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    [paragraph setLineBreakMode:NSLineBreakByClipping];
    [__badgeString drawInRect:bounds withAttributes:@{ NSFontAttributeName:font,
                                                       NSParagraphStyleAttributeName:paragraph}];
#else
    [__badgeString drawInRect:bounds withFont:font lineBreakMode:TDLineBreakModeClip];
#endif

I was thinking about adding the following lines:

#if !__has_feature(objc_arc)
    [paragraph release];
#endif

So final code will look as follows. What do you think?

#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    [paragraph setLineBreakMode:NSLineBreakByClipping];
    [__badgeString drawInRect:bounds withAttributes:@{ NSFontAttributeName:font,
                                                       NSParagraphStyleAttributeName:paragraph}];
#if !__has_feature(objc_arc)
    [paragraph release];
#endif

#else
    [__badgeString drawInRect:bounds withFont:font lineBreakMode:TDLineBreakModeClip];
#endif