Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using NSDate in While loop

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.

like image 971
Wasim Avatar asked Dec 13 '22 12:12

Wasim


1 Answers

In line NSDate *currentDate = [[NSDate alloc] init]; you create a new object, which you should release. In line currentDate = [NSDate date]; you do not release an old object, you only make a pointer to point to another object. In line [currentDate release]; you release an object created on the second line of a loop, which may cause an error (that object is marked as autorelease one and iOS will clean it for you). You should rewrite your code like:

while (interval > 0.0) {
      NSDate *currentDate =  [NSDate date];
      interval = (float) [newDate timeIntervalSinceDate: currentDate] / 60;
}
like image 52
Konstantin Chugalinskiy Avatar answered Jan 05 '23 06:01

Konstantin Chugalinskiy