Mausimo
Mausimo

Reputation: 8168

why is the NSMutableArray is being destroyed in this loop?

It is the arrayOfPerformances that is getting broken.

here is the .h for the array:

    NSMutableArray * arrayOfPerformances;
}
@property (nonatomic, retain) NSMutableArray * arrayOfPerformances;

and the .m that has the loop:

[dataArray release];
[dataDictionary release];
dataArray = [[NSMutableArray alloc] init];
dataDictionary = [[NSMutableDictionary alloc] init];

NSDate * CurrentDate = start;
int i = 0;

NSMutableArray * arrayOfPerformancesForCurrentDate = [[NSMutableArray alloc] init];
while(YES)
{
    i = 0;
    if ([arrayOfPerformancesForCurrentDate count] > 0)
    {
        [arrayOfPerformancesForCurrentDate removeAllObjects];   
    }

    for (i; i < self.arrayOfPerformances.count; i++)
    {
        Performance * performanceItem = [[Performance alloc] init]; 
        performanceItem = [self.arrayOfPerformances objectAtIndex:i];

        NSString * sPerformanceDate = [performanceItem.sDate substringToIndex:10];
        NSString * sCurrentDate = [CurrentDate dateDescription];

        if([sPerformanceDate isEqualToString:sCurrentDate])
        {
            [arrayOfPerformancesForCurrentDate addObject:performanceItem];
        }

        [performanceItem release];
    }

    if ([arrayOfPerformancesForCurrentDate count] >= 1)
    {
        [dataDictionary setObject:arrayOfPerformancesForCurrentDate forKey:CurrentDate];
        [dataArray addObject:[NSNumber numberWithBool:YES]];
    }
    else
    {
        [dataArray addObject:[NSNumber numberWithBool:NO]];
    }

    TKDateInformation info = [CurrentDate dateInformation];
    info.day++;
    CurrentDate = [NSDate dateFromDateInformation:info];
    if([CurrentDate compare:end]==NSOrderedDescending) break;
}

Any help would be appreciated. I dont understand why this is happening?

Upvotes: 0

Views: 134

Answers (2)

raid5ive
raid5ive

Reputation: 6642

I don't think arrayOfPerformances of being destroyed. It appears you aren't even initializing it anywhere.

Upvotes: 0

user467105
user467105

Reputation:

This part doesn't look right:

Performance * performanceItem = [[Performance alloc] init];     <--
performanceItem = [self.arrayOfPerformances objectAtIndex:i];   <--

NSString * sPerformanceDate = [performanceItem.sDate substringToIndex:10];
NSString * sCurrentDate = [CurrentDate dateDescription];

if([sPerformanceDate isEqualToString:sCurrentDate])
{
    [arrayOfPerformancesForCurrentDate addObject:performanceItem];
}

[performanceItem release];                                      <--

You alloc+init performanceItem but then set it to an object in arrayOfPerformances and then you release it (while it's pointing to the object in arrayOfPerformances).

Change that section to this:

Performance *performanceItem = [self.arrayOfPerformances objectAtIndex:i];

NSString * sPerformanceDate = [performanceItem.sDate substringToIndex:10];
NSString * sCurrentDate = [CurrentDate dateDescription];

if([sPerformanceDate isEqualToString:sCurrentDate])
{
    [arrayOfPerformancesForCurrentDate addObject:performanceItem];
}

//don't release performanceItem

Upvotes: 5

Related Questions