问题描述
我想在While循环中使用[NSDate date]获取当前日期。我这样做这样做:
I want to get the current date using [NSDate date] in a While Loop. I accomplish this by doing like this:
while (interval > 0.0) {
NSDate *currentDate = [[NSDate alloc] init];
currentDate = [NSDate date];
interval = (float) [newDate timeIntervalSinceDate: currentDate] / 60;
[currentDate release];
}
我不知道为什么内存泄漏显示有大量内存泄漏。请指导我,正确的方式来完成我的任务。
I dont know why is the Memory leaks shows that there is a great amount of memory is leaked. Kindly guide me that what is the right way to accomplish my task.
推荐答案
问题不在于你泄漏本身,
The problem is not that you are leaking per se but that you are running in a while loop.
自动发布的日期在自动释放池中增长,因为池只在运行循环的空闲时间内空闲。
The auto released dates are growing in the autorelease pool because the pool only empties in the idle time on the run loop.
一个解决方案是在while的范围内创建一个本地自动释放池
One solution is to create a local autorelease pool within the scope of the while
while (foo) {
NSAutoreleasePool *aPool = [[NSAutoreleasePool alloc ] init];
NSDate *currentDate = [NSDate date];
// other computational foo
[aPool release]
}
当您在本地范围内释放池时,它将立即删除您请求的自动释放日期。
When you release the pool in the local scope it will immediately drop the autoreleased date you requested.
这篇关于在While循环中使用NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!