views:

391

answers:

4

Hi, 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

+1  A: 

You're properly releasing pagamento_ after adding it to the dados array, but you're returning a copy of dados from this method. That is most likely a mistake. Unless the caller of this method knows to release that array, it will be leaked, causing the pagamento_ object to be leaked as well. You probably should do this:

return [[dados copy] autorelease];

kperryua
but when i do this, my app exit =/
Helena
+1  A: 

You almost certainly want to return [[dados copy] autorelease]. Just returning the result of copy will normally be a memory leak under the Cocoa memory management rules.

Chuck
thanks to anwser... but when i do this, my app exit....
Helena
Then it sounds like you have another error.
Chuck
yes chuck... thank u very much
Helena
A: 

Try using the LLVM Clang Static analyser, for details see this link

Ram
Thank u very much!
Helena
+3  A: 
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.

bbum
thank u very much... I needed to retain a place in another part of the code to work :)
Helena