我有一个小项目,我注意到我的代码中有很多漏洞。所以我开始使用自动释放。但有时,我只是无法摆脱泄漏或使用自动释放导致分析器告诉自动释放太多。这是一个返回类属性标题的函数,其中包含字体、阴影和与当前秒的对齐。我一直在玩它一段时间,试图意识到我做错了什么。有人可以看看它并告诉我哪里出错了吗?
- (NSAttributedString *)attributedTitle {
NSMutableParagraphStyle *pStyle = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
[pStyle setAlignment: NSCenterTextAlignment];
NSShadow *pShadow = [[[NSShadow alloc] init] autorelease];
[pShadow setShadowColor: [NSColor colorWithSRGBRed:0.11 green: 0.11 blue:0.11 alpha: 0.67]];
[pShadow setShadowBlurRadius: 1.0];
[pShadow setShadowOffset: NSMakeSize(0,1)];
[pShadow set];
NSMutableDictionary *attributes = [[[[NSMutableDictionary alloc] initWithObjectsAndKeys:
[NSFont fontWithName: @"Arial Bold" size: 11], NSFontAttributeName,
[NSColor colorWithDeviceWhite: 1.0 alpha: 0.83], NSForegroundColorAttributeName,
pStyle, NSParagraphStyleAttributeName,
pShadow, NSShadowAttributeName,
nil] mutableCopy] autorelease];
NSString *text = [[[NSString alloc] initWithFormat: @"%d", [[[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar]
components: NSSecondCalendarUnit fromDate: [[NSDate alloc] init]]second]] autorelease];
NSAttributedString *result = [[[NSAttributedString alloc] initWithString: text attributes: attributes] autorelease];
return result;
}
起初,这被缩短了很多 - 如果我不能使用变量,我不会使用变量,但我添加了变量并且它变得更长 - 所以这实际上只是泄漏和自动释放的一个例子。
我很确定这也可以做得更简单,但是现在我担心正确使用自动释放以及如何消除泄漏。以及为什么我有泄漏(我还有更多的代码)如果我意识到出了什么问题,我可以修复..)
好的,我把它整理好了..没有泄漏 - 缩短了..
- (NSAttributedString *)attributedTitle {
NSMutableParagraphStyle *pStyle = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
[pStyle setAlignment: NSCenterTextAlignment];
NSShadow *pShadow = [[[NSShadow alloc] init] autorelease];
[pShadow setShadowColor: [NSColor colorWithSRGBRed:0.11 green: 0.11 blue:0.11 alpha: 0.67]];
[pShadow setShadowBlurRadius: 1.0];
[pShadow setShadowOffset: NSMakeSize(0,1)];
[pShadow set];
NSDictionary *attributes = [[[NSDictionary alloc] initWithObjectsAndKeys:
[NSFont fontWithName: @"Arial Bold" size: 11], NSFontAttributeName,
[NSColor colorWithDeviceWhite: 1.0 alpha: 0.83], NSForegroundColorAttributeName,
pStyle, NSParagraphStyleAttributeName,
pShadow, NSShadowAttributeName,
nil] autorelease];
NSDateComponents *dc = [[[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar] autorelease]
components: NSSecondCalendarUnit fromDate: [[[NSDate alloc] init] autorelease]];
return [[[NSAttributedString alloc] initWithString: [[[NSString alloc] initWithFormat: @"%d", [dc second]] autorelease] attributes: attributes] autorelease];
}
但是那又如何呢?
SInt32 CFWeeksInYear(NSUInteger year)
{
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:@"dd.MM.YYYY"];
NSDate *tempMonth = [[[NSDate alloc] initWithString: [[[NSString alloc] initWithFormat: @"31.12.%ld", year] autorelease]] autorelease];
SInt32 result = CFAbsoluteTimeGetWeekOfYear([tempMonth timeIntervalSinceReferenceDate], CFTimeZoneCopyDefault());
return result;
}
SInt32 CFWeekOfYear(CFGregorianDate tempMonth)
{
CFAbsoluteTime tempDate = CFGregorianDateGetAbsoluteTime (tempMonth, CFTimeZoneCopyDefault());
return CFAbsoluteTimeGetWeekOfYear(tempDate, CFTimeZoneCopyDefault());
}
它泄漏在:
SInt32 result = CFAbsoluteTimeGetWeekOfYear([tempMonth timeIntervalSinceReferenceDate], CFTimeZoneCopyDefault());
和:
CFAbsoluteTime tempDate = CFGregorianDateGetAbsoluteTime (tempMonth, CFTimeZoneCopyDefault());
甚至在:
return CFAbsoluteTimeGetWeekOfYear(tempDate, CFTimeZoneCopyDefault());
几乎就像它与 CFTimeZoneCopyDefault() 有关但它不应该返回一个指针..