我的应用中存在泄漏问题。
我正在尝试向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课程中我可以做到
[tab_Demandes removeAllObjects]
TableViewController中的。
但是后来我为每个对象Demande泄露了属性。这就像当我发布[dem release]时,属性也被发布了,当我想从我的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;
}
答案 0 :(得分:0)
tab_Demandes
被初始化的地方?
仅仅在.h文件中声明它是不够的,你还必须告诉编译器分配并初始化它。
一个简单的修复可能是用
替换行[tab_Demandes removeAllObjects];
if(!tab_Demandes) { // tab_Demandes is nil and has not previously been created
tab_Demandes = [[NSMutableArray arrayWithCapacity:10] retain];
} else {
[tab_Demandes removeAllObjects];
}
不要忘记[tab_Demandes release]
方法
dealloc