views:

1411

answers:

1

Hi, I'm sure this is memory related but I can't seem to figure out what variable is causing me problems.

I'm releasing everything I allocate as far as I can tell. This loop seems to work fine if it runs 365 times but not 730 (its doing days in a year).

Here is the snippet of code that is failing.. it never gets out of the loop when the program ends on the device.

Any help is greatly appreciate. Thanks!

NSMutableArray *dtArray = [[NSMutableArray alloc] init];

    double frequencyInterval = [Frequency doubleValue];
    int i;

    NSString *newDateString = @"";
    NSString *sqlString = [NSString stringWithFormat:@"insert into ToDoList (TaskId, DueDate, Completed, Notes, Status, CompletedDate, Frequency) values ('%i', \"%@\", '0',\"%@\",'' ,'0', %@);", (int)self.AddTaskId, origDueDateString, NotesField, todoFrequency];
    NSDate *nDate = dueDate;
    NSDateFormatter *f3 = [[NSDateFormatter alloc] init];
    [f3 setDateFormat:@"yyyy-MM-dd"];

    for(i=0; i< 1000; i++) {

         newDate = [newDate addTimeInterval:(frequencyInterval*86400)];
        newDateString = [f3 stringFromDate:newDate];

        if ([endRecurDate compare:newDate] == NSOrderedDescending)
        {
            [dtArray addObject:newDate];
            if (i == 0)
            {
                sql = [sqlString stringByAppendingString:[NSString stringWithFormat:@"insert into ToDoList (TaskId, DueDate, Completed, Notes, Status, CompletedDate, Frequency) values ('%i', \"%@\", '0',\"%@\",'' ,'0', %@)", (int)self.AddTaskId, newDateString, NotesField, todoFrequency]];
            }
            else
            {
                sql = [sql stringByAppendingString:[NSString stringWithFormat:@";insert into ToDoList (TaskId, DueDate, Completed, Notes, Status, CompletedDate, Frequency) values ('%i', \"%@\", '0',\"%@\",'' ,'0', %@)", (int)self.AddTaskId, newDateString, NotesField, todoFrequency]];
            }
            //stringByAppendingString

        }
        else
        {
            break;
        }

    }
    [dtArray release];
    [f3 release];
+2  A: 

For one thing, you don't need to inialize newDateString to @""; simply declaring it there is enough.

I didn't notice any obvious memory leaks, but you are looping through the for statement 1,000 times. All of your autoreleased objects may not be released until the entire loop is done, so that may be why you're using too much memory. To release them during the loop, do this:

for(i=0; i< 1000; i++) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    /* your code here */

    [pool drain];
}

Play with this a little bit; you probably don't need to drain the pool every single time your loop executes, but you might build a counter and drain the pool every 25 loops or so.

Jeff Kelley
Maybe I'm not approaching this correctly, but I'm not autoreleasing any objects in this loop.
maxpower
You aren't, but a lot of the methods you're calling are returning autoreleased objects. Every time you call stringByAppendingString, stringFromDate, etc., the objects returned are autoreleased. By my count there are six objects inside the loop, and depending on the length of the strings this is almost certainly the issue.
Jeff Kelley