Search code examples
objective-cios6memory-leaksxcode-instruments

How to solve Memory leak in SKDatabase function?


Instrument is showing memory leak in following code. I have made sure everywhere retain count is maintained. I have also added autorelease pool, still memory leak is there. How to solve this?

Code Block:

- (NSArray *)lookupAllForSQL:(NSString *)sql
{
    sqlite3_stmt *statement;
    id result = nil;
    NSMutableArray *thisArray = [NSMutableArray arrayWithCapacity:4];
    statement = [self prepare:sql];
    if (statement)
    {
        while (sqlite3_step(statement) == SQLITE_ROW)
        {
            NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

            NSMutableDictionary *thisDict = [NSMutableDictionary dictionaryWithCapacity:4];
            for (int i = 0 ; i < sqlite3_column_count(statement) ; i++)
            {
                NSAutoreleasePool *poolInside = [[NSAutoreleasePool alloc] init];
                if(sqlite3_column_type(statement,i) == SQLITE_NULL)
                {
                    continue;
                }
                if (sqlite3_column_decltype(statement,i) != NULL &&
                    strcasecmp(sqlite3_column_decltype(statement,i),"Boolean") == 0)
                {
                    result = [NSNumber numberWithBool:(BOOL)sqlite3_column_int(statement,i)];
                }
                else if (sqlite3_column_type(statement,i) == SQLITE_INTEGER)
                {
                    result = [NSNumber numberWithInt:(int)sqlite3_column_int(statement,i)];
                }
                else if (sqlite3_column_type(statement,i) == SQLITE_FLOAT)
                {
                    result = [NSNumber numberWithFloat:(float)sqlite3_column_double(statement,i)];
                }
                else
                {
                    if((char *)sqlite3_column_text(statement,i) != NULL)
                    {
                        //result = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,i)];
                        [thisDict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,i)]
                                     forKey:[NSString stringWithUTF8String:sqlite3_column_name(statement,i)]];
                        //[result release];
                        result = nil;
                    }
                }
                if (result)
                {
                    [thisDict setObject:result forKey:[NSString stringWithUTF8String:sqlite3_column_name(statement,i)]];
                }
                [poolInside drain];
            }
            [thisArray addObject:[NSDictionary dictionaryWithDictionary:thisDict]];
            [pool drain];
        }
    }
    sqlite3_finalize(statement);
    return thisArray;
}

Instrument Screenshot:

enter image description here


Solution

  • I got the solution. This function was returning an NSArray, which I was not releasing properly. So, all those variables were causing memory leak not this function.

    This video helped me understand that.