R – Memory Leak – iPhone

iphonememory-leaksobjective c

im having problems with leak memory. The instruments show line "pagamento_" is a malloc. i dont have idea how resolve this.

i have following code: * dados is NSMutableArray, and im alloc in other place… and do release in dealloc.

NSString *path = [self caminho];

if (sqlite3_open([path UTF8String], &Banco) == SQLITE_OK){

    if (sqlite3_prepare_v2(Banco, [sql UTF8String], -1, &stmt, NULL) == SQLITE_OK) {


        int row = sqlite3_step(stmt);
        while(row == SQLITE_ROW) {
                            ...

                           if([tabela isEqual:@"Pagamento"]){

                pagamento_ = [[Pagamento alloc]init];

                pagamento_.codigo = sqlite3_column_int(stmt, 0);
                pagamento_.codNomePgto = sqlite3_column_int(stmt, 1);
                pagamento_.codCategoria = sqlite3_column_int(stmt, 2);
                pagamento_.vencimento = [NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 3)];
                pagamento_.repeticaoPagamento = [NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 4)];
                pagamento_.dataTermino = [NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 5)];
                pagamento_.vctoFDS = [NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 6)];
                pagamento_.valorPrevisto = [NSString stringWithFormat:@"%4.2f",sqlite3_column_double(stmt, 7)];
                pagamento_.valorPago = [NSString stringWithFormat:@"%4.2f",sqlite3_column_double(stmt, 8)];
                pagamento_.dataPgto = [NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 9)];
                pagamento_.anotacoes =[NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 10)];
                pagamento_.debitoAutomatico = [NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 11)];
                pagamento_.nome = [NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 12)];
                pagamento_.numSerie = sqlite3_column_int(stmt, 13);
                pagamento_.codFavorecido = sqlite3_column_int(stmt, 14);
                pagamento_.favorecido =[NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 15)];
                pagamento_.valor = [NSString stringWithFormat:@"%4.2f",sqlite3_column_double(stmt, 16)];

                [dados addObject:pagamento_];

                [pagamento_ release];

            }

                     row = sqlite3_step(stmt);

        }

sqlite3_finalize(stmt);
sqlite3_close(Banco); 

return [dados copy];

anybody know how i resolve this??
thanks

Best Answer

return [dados copy];

This is causing a leak since you aren't releasing the original array. Since dados is an NSMutbaleArray that is, in theory, allocated in your -init and released in your -dealloc, copying dados on return from the code above is a good idea, but you should use:

return [[dados copy] autorelease];

If your application is crashing when you just return dados (or when you do the above), it is because you aren't managing memory correctly. As suggested by Ram, use the static analyzer and fix any problems it identifies first (build and analyze in Xcode on Snow Leopard).

If your app still crashes, then turn on NSZombies (google can show you how) and see if that catches it.

Unless you need to target iPhone OS 2.x or have a really really esoteric need to do so, you should use Core Data instead of SQLite. It'll undoubtedly be faster and save you significant development time.

Related Topic