iphone - 为什么这段 Objective C 代码会泄漏内存?

标签 iphone objective-c cocoa

已更新

我在 Objective C 中有这个方法:

-(NSDate*)roundTo15:(NSDate*)dateToRound {
    int intervalInMinute = 15;
    // Create a NSDate object and a NSDateComponets object for us to use
    NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSMinuteCalendarUnit fromDate:dateToRound];

    // Extract the number of minutes and find the remainder when divided the time interval
    NSInteger remainder = [dateComponents minute] % intervalInMinute; 
    // gives us the remainder when divided by interval (for example, 25 would be 0, but 23 would give a remainder of 3

    // Round to the nearest 5 minutes (ignoring seconds)
    if (remainder >= intervalInMinute/2) {
        dateToRound = [dateToRound dateByAddingTimeInterval:((intervalInMinute - remainder) * 60)]; // Add the difference
    } else if (remainder > 0 && remainder < intervalInMinute/2) {
        dateToRound = [dateToRound dateByAddingTimeInterval:(remainder * -60)]; // Subtract the difference
    }

    return dateToRound;
}

这就是我调用该方法的方式:

item.timestamp = 
    [self roundTo15:[[NSDate date] dateByAddingTimeInterval:60 * 60]];

当执行以下行时,Instruments 说我泄漏了 NSDate 对象:

dateToRound = [dateToRound dateByAddingTimeInterval:(remainder * -60)];

所以这是我的项目对象,我需要使用新的更正的 NSDate 进行更新。我尝试创建一个 roundedDate 并像这样返回它:return [roundedDate autorelease];,但随后出现了错误的访问错误。

最佳答案

问题在于 dateToRound 作为对一个对象的引用传入,而您将其设置为对另一个对象的引用。原始对象现已被废弃并已泄露。

您应该创建一个新的 NSDate * 并返回它,而不是重新分配 dateToRound

示例代码:

-(NSDate*)roundTo15:(NSDate*)dateToRound {
    int intervalInMinute = 15;
    // Create a NSDate object and a NSDateComponets object for us to use
    NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSMinuteCalendarUnit fromDate:dateToRound];

    // Extract the number of minutes and find the remainder when divided the time interval
    NSInteger remainder = [dateComponents minute] % intervalInMinute; // gives us the remainder when divided by interval (for example, 25 would be 0, but 23 would give a remainder of 3

    // Round to the nearest 5 minutes (ignoring seconds)
    NSDate *roundedDate = nil;
    if (remainder >= intervalInMinute/2) {
        roundedDate = [dateToRound dateByAddingTimeInterval:((intervalInMinute - remainder) * 60)]; // Add the difference
    } else if (remainder > 0 && remainder < intervalInMinute/2) {
        roundedDate = [dateToRound dateByAddingTimeInterval:(remainder * -60)]; // Subtract the difference
    } else {
        roundedDate = [[dateToRound copy] autorelease];
    }

    return roundedDate;
}

关于iphone - 为什么这段 Objective C 代码会泄漏内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3825409/

相关文章:

iphone - 从异步 url 连接返回数据给调用者

cocoa-touch - Cocoa 中的流类支持查找吗?

iphone - 我如何在 UIView 手势下检测 UIButton 的 TouchUp 事件

iphone - 任何基于 Windows 的 IDE 来查看 objective-c 代码?

objective-c - 向 Mac 应用程序添加滚动的最简单方法

objective-c - 寻找类似 objective c NSList 的选项,(我知道之前有人在这里问过),你能提供什么例子?

cocoa - 隐藏核心数据高级设置警告

ios - 在 iOS WebView 中获取 PDF 文档高度

iphone - Apple 推送通知服务 - 设备上没有通知

ios - viewWithTag 可以使用有限的时间