Memory leak in NSCalendar?

Hi,


hopefully I'm doing something horribly wrong, but it seems like there is a memory leak in NSCalendar.components. Simply calling the method will leak something. Doing it in a loop will show it better:


        let calendar = NSCalendar.currentCalendar()
        let startDate = calendar.dateWithEra(1, year: 2015, month: 10, day: 4, hour: 12, minute: 0, second: 0, nanosecond: 0)!
        var i = 0
        var j = 0
        for(j = 0; j < 1000000; j++) {
            for(i = 0; i < 1000000; i++) {
                let components:NSDateComponents = calendar.components(NSCalendarUnit.Hour, fromDate: startDate)  // LEAK!
            }
        }

I would expect that components goes out of scope and is cleaned up, but while the loops are running memory will go only up.


I've tested on native iOS 9 and the simulator, but both show the same behaviour.


What am I doing wrong???


Thanks

Try adding autoreleasepool { } around the loop body. NSCalendar's components(fromDate:) method returns an autoreleased Objective-C object, which in the absence of an explicit pool, will be released the next time the containing pool (probably the main runloop's pool) is drained.


This usually isn't an issue, but if you're generating huge numbers of autoreleased objects, it can be useful to use an autoreleasepool to bound their lifetime a little more tightly.

Thank you so much!!! Looks like this part I hadn't seen in the manuals yet.

Memory leak in NSCalendar?
 
 
Q