Leak of Malloc 16 Bytes in C / Objective-C

When I profile my code with Instruments, it shows a leak of Malloc 16 bytes from this function (below), but I never used malloc in this function. Is there a place in this function where I should free some resources?

It may look like a lot of code, but there is really only the variables counts and counts2 as possible offenders I think.

+ (int) trimArray: (NSMutableArray*) source above: (short) max andMin: (short) min
{
    int counts[6][7];
    int counts2[6][7];
    for (int i=0;i<=5;i++)
    {
        for (int ii=0;ii<7;ii++)
        {
            counts[i][ii] = 0;
            counts2[i][ii] = 0;
        }
    }


    int capacity = (int)[source count]/max;
    if (capacity <2)
        capacity = 2;

    NSMutableArray *itemsToRemove = [[NSMutableArray alloc] initWithCapacity:capacity];

    int week,dow,count1,count2;
    EntryTimeItem *item;
    NSEnumerator *e;

    e = [source objectEnumerator];
    while (item = [e nextObject])
    {
        week = item.week_number;
        dow  = item.day_of_the_week;
        if (week >=0 && week <6 && dow >=0 && dow <7)
        {
            counts[week][dow]++;
        }
    }

    e = [source objectEnumerator];
    while (item = [e nextObject])
    {
        week = item.week_number;
        dow  = item.day_of_the_week;
        if (week >= 0 && week < 6 && dow >= 0 && dow < 7)
        {
            count2 = counts2[week][dow];
            count1 = counts[week][dow];
            if (count1 > max)
            {
                if (!count2)
                {
                    item.time = -1;
                    item.align = NSCenterTextAlignment;
                    item.label = [NSString stringWithFormat:@"%d entries",count1];
                }
                else {
                    // remove this item if it is after the first item which
                // was converted to a placeholder for all the items
                    [itemsToRemove addObject:item];
                }
            }
            counts2[week][dow]++;
        }
    }

    e = [itemsToRemove objectEnumerator];
    while (item = [e nextObject])
    {
        [source removeObject:item];
    }

    int count_extra_events = 0;
    for (int i=0;i<7;i++)
    {
        int count_events2 = 0;
        for (int ii = 0; ii < 6; ii++)
        {
            int count3 = counts[ii][i];
            if (count3 < max && count3 > min)
                count_events2 += count3 - min;
        }
        // store the greatest value found sofar
        if (count_events2 > count_extra_events)
        {
            count_extra_events = count_events2;
        }
    }

    return count_extra_events;
}

Answers


The problem appears to stem from the line:

NSMutableArray *itemsToRemove = [[NSMutableArray alloc] initWithCapacity:capacity];

Please check if there is anyway, the resource itemsToRemove can be freed.


Need Your Help

How should I structure this class inheritance?

oop inheritance coldfusion

I'm developing an application for transferring databases and directories of user-uploaded images/documents from our production server to the development server. The application is written in Coldfu...

Symfony Propel Query

symfony1 propel

I want to make this query on Symfony/Propel