views:

81

answers:

1

Hi guys,

i have a leak issue in my app.

I'm trying to add and remove objects to a NSMutableArray.

Here is the class Demande :

@interface Demande : NSObject { 
//attibuts de la classe demande
NSString *demId;
NSString *demStatut;
NSString *demTitle;
NSString *demCreated;
NSString *demIdCopro;
NSString *demIdImmeuble;
NSString *demIdLot;
NSString *demDescriptif;
NSString *demAuteur;
NSString *demIdAuteur;
NSString *demLoginAuteur;   
}
@property (retain,nonatomic) NSString *demId;
@property (retain,nonatomic) NSString *demIdCopro;
@property (retain,nonatomic) NSString *demIdImmeuble;    
@property (retain,nonatomic) NSString *demIdLot;
@property (retain,nonatomic) NSString *demDescriptif;
@property (retain,nonatomic) NSString *demStatut;
@property (retain,nonatomic) NSString *demTitle;
@property (retain,nonatomic) NSString *demCreated;
@property (retain,nonatomic) NSString *demAuteur;
@property (retain,nonatomic) NSString *demIdAuteur;
@property (retain,nonatomic) NSString *demLoginAuteur; 
@end


#import "Demande.h"
@implementation Demande

@synthesize demId,demIdCopro,demIdImmeuble,demIdLot,demDescriptif,demStatut,demTitle,demCreated,demAuteur,demIdAuteur,demLoginAuteur;

- (void)dealloc {   

[demId release];
[demIdCopro release];
[demId release];
[demIdImmeuble release];
[demIdLot release];
[demDescriptif release];
[demStatut release];
[demTitle release];
[demCreated release];
[demAuteur release];
[demIdAuteur release];
[demLoginAuteur release];    
[super dealloc];

}
@end

What i try to do in MyTableViewController :

@interface MyTableViewController : UITableViewController {

IBOutlet UITableView *tableV;
NSMutableArray *tab_Demandes;   
}
@end

@implementation MyTableViewController

- (void)requestDReloadDone:(ASIHTTPRequest *)request
{   

NSData *responseData = [request responseData];  

NSString *jsonString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
//NSLog(@"JSON: %@",jsonString);

NSDictionary *results = [jsonString JSONValue];

NSArray *demandes = [results objectForKey:@"Demandes"]; 

    [tab_Demandes removeAllObjects]; // -------- CRASH HERE !!!, except the first time through the method  because the array is empty  

    for (NSDictionary *demandeD in demandes)
{

    //parse du JSON
    NSString *titre = [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"];

    //Creation de l'objet
    Demande *dem =[[Demande alloc] init];       
    dem.demTitle=titre;
    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 addObject:dem];

    [dem release];
}

[tableV reloadData];

[jsonString release];
}

On the first launch of the app my array tab_Demandes is empty so everything is fine [removeAllObjects] works as there are no objects in it. When i reload the tab_Demandes i need to clear it. That's when the app crashes

I realised that if i do :

- (void)dealloc {   
     /*
[demId release];
[demIdCopro release];
[demId release];
[demIdImmeuble release];
[demIdLot release];
[demDescriptif release];
[demStatut release];
[demTitle release];
[demCreated release];
[demAuteur release];
[demIdAuteur release];
[demLoginAuteur release];
     */  
[super dealloc];
}

in the Demande class i then can do

[tab_Demandes removeAllObjects]

in TableViewController.

But then i have the properties as leaked for each object Demande. It's like when i do [dem release] the properties are also rleased and when i want to do [removeAllObjects] from my tab_Demandes there are no properties to release and i get a Bad_Access.

If anyone has an idea ...

EDIT: cellForROw method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    

static NSString *CellIdentifier = @"Cell";    
CustomCellViewController *cell = (CustomCellViewController *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{
   // NSLog(@"Cell created");
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCellViewController" owner:nil options:nil];

    for(id currentObject in topLevelObjects)
    {
        if([currentObject isKindOfClass:[CustomCellViewController class]])
        {
            cell = (CustomCellViewController *)currentObject;
            break;
        }
    }
}

    // Set up the cell...   

    cell.lbl_demId.text=[NSString stringWithFormat:@"%@", [[tab_Demandes objectAtIndex:indexPath.row ] demId]]; 
    cell.lbl_demCopro .text=[NSString stringWithFormat:@"%@", [[tab_Demandes objectAtIndex:indexPath.row] demIdCopro ]];
    cell.lbl_demImmeuble.text=[NSString stringWithFormat:@"%@", [[tab_Demandes objectAtIndex:indexPath.row] demIdImmeuble]];
    cell.lbl_demLot.text=[NSString stringWithFormat:@"%@", [[tab_Demandes objectAtIndex:indexPath.row] demIdLot]];
    cell.lbl_demDescriptif.text=[NSString stringWithFormat:@"%@", [[tab_Demandes objectAtIndex:indexPath.row] demTitle]];
    if ([[[tab_Demandes objectAtIndex:indexPath.row] demStatut] isEqualToString: @"Validée"])
    {
    cell.lbl_ImageView.image=[UIImage imageNamed:@"VDEM2.png"];
    }
    if([[[tab_Demandes objectAtIndex:indexPath.row] demStatut] isEqualToString: @"Initiée"])
    {
       cell.lbl_ImageView.image=[UIImage imageNamed:@"IDEM.png"];
    }
    if([[[tab_Demandes objectAtIndex:indexPath.row] demStatut] isEqualToString: @"Terminée"])
    {
    cell.lbl_ImageView.image=[UIImage imageNamed:@"TDEM.png"];
    }
    if([[[tab_Demandes objectAtIndex:indexPath.row] demStatut] isEqualToString: @"En coursée"])
    {
    cell.lbl_ImageView.image=[UIImage imageNamed:@"EDEM.png"];

    }

return cell;       
} 
A: 

Where it tab_Demandes being intialized?

It's not enough to only declare it in the .h file, you must also tell the compiler to alloc and init it.

A simple fix might be replacing the line [tab_Demandes removeAllObjects]; with

if(!tab_Demandes) { // tab_Demandes is nil and has not previously been created
    tab_Demandes = [[NSMutableArray arrayWithCapacity:10] retain];
} else {
    [tab_Demandes removeAllObjects];
}

Don't forget to [tab_Demandes release] in your dealloc method

TomH