我有我的應用程序泄漏問題。NSMutbleArray - 添加/刪除與屬性的對象 - 泄漏
我想添加和刪除對象到NSMutableArray。
這裏是類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
我嘗試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];
}
在首批推出的應用我的數組tab_Demandes是空的,所以一切都很好[removeAllObjects ]工作,因爲它沒有任何對象。 當我重新加載tab_Demandes我需要清除它。
- (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];
}
在Demande類我則可以在TableViewController做
[tab_Demandes removeAllObjects]
:當應用程序崩潰
我意識到,如果我這樣做的。
但後來我有屬性泄漏每個對象Demande。這就像當我做[dem release]時,屬性也是rleased,當我想要從我的tab_Demandes [removeAllObjects]中執行時,沒有屬性可以發佈,並且我得到一個Bad_Access。
如果任何人有一個想法......
編輯:cellForROw方法:
- (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;
}
你有沒有試過非原子,保留,而不是保留,非原子。我知道這聽起來很愚蠢,但以防萬一...... – VdesmedT 2010-09-24 12:15:45
嘿VdesmedT ;-)。我試過非原子,保留,沒有改變任何東西。我需要在將它們添加到tab_Demandes後立即釋放dem對象嗎?這是一個正確的方法嗎?當我釋放對象時是否釋放對象屬性? – wallou 2010-09-24 12:23:18
tab_Demandes只有指向dem對象的指針。如果我做dem釋放也許如果屬性被釋放,當我嘗試做removeAllObjects也許指針仍然參照已經發布的屬性? – wallou 2010-09-24 12:27:32