1

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.

3
  • how do you add objects to array? Commented Sep 17, 2010 at 15:47
  • 1
    Sounds like you over-released at least one of the objects you added. Without you showing some sample code its impossible to know though. Commented Sep 17, 2010 at 16:26
  • i've just added the code Commented Sep 20, 2010 at 7:48

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

The Zombies instrument in Instruments may be very helpful, too.
is there a risk of memory leak by not releasing the objects right after added them to the tab?
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.