views:

84

answers:

1

Hi guys,

i am trying to remove objects from my NSMutableArray but i keep getting bad_access error.

I used "removeAllObjects" and "removeObjectAtIndex" but none of these work.

If i add objects to this array , everything is fine.

What i want to do is to clear my array before filling it again.

I didnt release the array before trying to fill.

Thx for the help.

Bye

code:

[tab_Demandes removeAllObjects];

for (NSDictionary *demandeD in demandes)
{

    NSInteger i=0;


    NSString *title = [demandeD objectForKey:@"Title"];
    NSString *desriptif = [demandeD objectForKey:@"Description"];
    NSString *Id = [demandeD objectForKey:@"Id"];
    NSString *created = [demandeD objectForKey:@"Created"];
    NSString *statut = [demandeD objectForKey:@"Statut"];
    NSString *copropriete = [demandeD objectForKey:@"Copropriete"];
    NSString *immeuble = [demandeD objectForKey:@"Immeuble"];
    NSString *lot = [demandeD objectForKey:@"Lot"];
    NSString *auteur = [demandeD objectForKey:@"Author"];
    NSString *auteurId = [demandeD objectForKey:@"IdAuthor"];
    NSString *auteurLogin = [demandeD objectForKey:@"Login"];


    Demande *dem =[[Demande alloc] init];       
    dem.demTitle=title;
    dem.demId=Id;
    dem.demCreated=created;

    NSString *descriptifDecode = [desriptif stringByReplacingPercentEscapesUsingEncoding:
                                  NSASCIIStringEncoding];       

    dem.demDescriptif=descriptifDecode;
    dem.demIdCopro=copropriete;
    dem.demIdImmeuble=immeuble;
    dem.demIdLot=lot;
    dem.demStatut=statut;
    dem.demAuteur=auteur;
    dem.demIdAuteur=auteurId;
    dem.demLoginAuteur=auteurLogin;


    //[tab_Demandes replaceObjectAtIndex:i withObject:dem];
    //i=i+1;



    [tab_Demandes addObject:dem];

    //[dem release];
}

When do i need to release the objects i add to the tab if i need to remove them later? because of memory leaks.

+1  A: 

It's not that you released the array, it's that one (or more) of the objects in the array was already released outside of the array. Try turning on NSZombieEnabled to see if you can figure out which one.

Chuck
The Zombies instrument in Instruments may be very helpful, too.
Max Seelemann
is there a risk of memory leak by not releasing the objects right after added them to the tab?
wallou
@wailou: There is a risk of a memory leak if you don't follow the memory management guidelines. That might mean you should release them right after adding them to the array, it might not. Depends on if you own them.
Chuck