0
NSDate *today = [[NSDate alloc] init];
    NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *onset = [[NSDateComponents alloc] init];
    [onset setMonth:monthsStart];
    NSDate *fromDate = [gregorian dateByAddingComponents:onset toDate:today options:0];
    [onset setMonth:monthsEnd];
    NSDate *toDate = [gregorian dateByAddingComponents:onset toDate:today options:0];

它说以下内容:-

  1. 方法返回一个具有 +1 保留计数的 Objective-C 对象
  2. 对象泄漏:分配和存储到today的对象在此执行路径中稍后未引用,并且保留计数为 +1
4

1 回答 1

0

泄漏是因为您没有释放today. 请使用[today release]和释放相同。calender和的情况也是如此onset。在行之后NSDate *toDate = [gregorian dateByAddingComponents:onset toDate:today options:0];,请立即释放所有这些参数。

NSDate *today = [[NSDate alloc] init];
NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *onset = [[NSDateComponents alloc] init];
[onset setMonth:monthsStart];
NSDate *fromDate = [gregorian dateByAddingComponents:onset toDate:today options:0];
[onset setMonth:monthsEnd];
NSDate *toDate = [gregorian dateByAddingComponents:onset toDate:today options:0];
//After you are done with the below variables, you can release it
[today release];
[calender release];
[onset release]; //you cannot use these variables after this line.

阅读此文档,高级内存管理编程指南

于 2012-11-29T08:03:31.507 回答