日期内容的内存泄漏(__NSCFCalendar,icu::GregorianCalendar)
我有一个小方法返回给定日期的第一个工作日:
- (NSDate*) getFirstDayOfTheWeekFor:(NSDate*)date {
NSCalendar *gregorianCalender = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *firstDayDate; //This is 100.0% leaking acc. to the Performance Tool Leaks
unsigned yearAndWeek = NSYearCalendarUnit | NSWeekCalendarUnit;
// retrieve the components from the current date
NSDateComponents *compsCurrentDate = [[gregorianCalender components:yearAndWeek fromDate:date] autorelease];
[compsCurrentDate setWeekday:2]; // Monday
[compsCurrentDate setHour:0];
[compsCurrentDate setMinute:0];
[compsCurrentDate setSecond:0];
// make a date from the modfied components
firstDayDate = [[gregorianCalender dateFromComponents:compsCurrentDate] autorelease];
return firstDayDate;
}
如您所见,我已经在尝试自动释放此处使用的每个变量(这不是我开始追踪之前的样子)泄漏)。最初我想在返回之前显式释放所有变量,除了“firstDayDate”变量,该变量由于返回而必须自动释放。
这些是性能工具发现的泄漏对象:
- icu::GregorianCalendar (1.00 KB)
- icu::SimpleTimeZone (112 字节)
- __NSDate (16 字节)
- icu::NSNumberingSystem (128 字节)
- __NSCFCalendar (48 字节)
错误一定是某些东西完全愚蠢,但我找不到它。你能帮助我吗?谢谢你!!
I am having a small method returning the first week-day of a given date:
- (NSDate*) getFirstDayOfTheWeekFor:(NSDate*)date {
NSCalendar *gregorianCalender = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *firstDayDate; //This is 100.0% leaking acc. to the Performance Tool Leaks
unsigned yearAndWeek = NSYearCalendarUnit | NSWeekCalendarUnit;
// retrieve the components from the current date
NSDateComponents *compsCurrentDate = [[gregorianCalender components:yearAndWeek fromDate:date] autorelease];
[compsCurrentDate setWeekday:2]; // Monday
[compsCurrentDate setHour:0];
[compsCurrentDate setMinute:0];
[compsCurrentDate setSecond:0];
// make a date from the modfied components
firstDayDate = [[gregorianCalender dateFromComponents:compsCurrentDate] autorelease];
return firstDayDate;
}
As you can see, I am already trying to autorelease every single variable that is used here (this is not what it looked like before I started to track down the leaks). Originally I wanted to explicitly release all variables before the return, except the "firstDayDate" variable, which HAS to be autoreleased because of the return.
These are the Leaked Objects found by the Performance Tool:
- icu::GregorianCalendar (1.00 KB)
- icu::SimpleTimeZone (112 Bytes)
- __NSDate (16 Bytes)
- icu::NSNumberingSystem (128 Bytes)
- __NSCFCalendar (48 Bytes)
The mistake MUST be something completely stupid but I can't find it. Can you help me? THANK YOU!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它应该看起来像这样:
如果firstDayDate 泄漏,则它不在这个方法中。检查下游。另外,ICU 部分对我来说看起来有点可疑。这可能是 icu 库周围的 iOS 包装器中的错误/泄漏。
请记住,如果您
alloc、init
或copy
,则只能release
或autorelease
。It should look like this:
If firstDayDate is leaking, it's not in this method. Check downstream. Also, the icu part looks a little fishy to me. It could be a bug/leak in iOS wrapper around the icu library.
Remember you only
release
orautorelease
if youalloc, init
, orcopy
.