我的应用中有内存泄漏。 我的应用程序在tableviewcontroller上启动。我解析一个json文件来填充数组。然后,此数组用于填充tableview的单元格。 我用对象(类)填充我的数组。
我做:
[tab_Demandes removeAllObjects];
for (NSDictionary *demandeD in demandes)
{
Demande *dem =[[Demande alloc] init];
dem.demTitle=title;
dem.demId=Id;
dem.demCreated=created;
[tab_Demandes addObject:dem];
[dem release];
}
我的数组和tableview在启动时填充,没问题。 如果我点击一个表格单元格,然后想要回到tableview上我的应用程序崩溃:
[tab_Demandes removeAllObjects];
如果我没有[dem release]我每次重新加载tab_Demandes时都会在乐器中泄漏。 RemoveAllObjects有效,但我有泄漏。
顺便提供任何帮助。
Wallou ++
编辑:这是我制作我的细胞的地方。也许我在创建细胞时搞砸了 实际上tab_Demandes在委托中声明,get / set从其他类(我加载tableviewcontroller)中访问它。
(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...
TabsAppDelegate *appDelegate = (TabsAppDelegate*)[[UIApplication sharedApplication] delegate];
cell.lbl_demId.text=[NSString stringWithFormat:@"%@", [[[appDelegate tab_Demandes] objectAtIndex:indexPath.row ] demId]];
cell.lbl_demCopro .text=[NSString stringWithFormat:@"%@", [[[appDelegate tab_Demandes] objectAtIndex:indexPath.row] demIdCopro ]];
cell.lbl_demImmeuble.text=[NSString stringWithFormat:@"%@", [[[appDelegate tab_Demandes] objectAtIndex:indexPath.row] demIdImmeuble]];
cell.lbl_demLot.text=[NSString stringWithFormat:@"%@", [[[appDelegate tab_Demandes] objectAtIndex:indexPath.row] demIdLot]];
cell.lbl_demDescriptif.text=[NSString stringWithFormat:@"%@", [[[appDelegate tab_Demandes] objectAtIndex:indexPath.row] demTitle]];
//NSLog(@"statut demande : %@",[[tab_Demandes objectAtIndex:indexPath.row] demStatut]);
if ([[[[appDelegate tab_Demandes] objectAtIndex:indexPath.row] demStatut] isEqualToString: @"Validée"])
{
cell.lbl_ImageView.image=[UIImage imageNamed:@"VDEM2.png"];
}
if([[[[appDelegate tab_Demandes] objectAtIndex:indexPath.row] demStatut] isEqualToString: @"Initiée"])
{
cell.lbl_ImageView.image=[UIImage imageNamed:@"IDEM.png"];
}
if([[[[appDelegate tab_Demandes] objectAtIndex:indexPath.row] demStatut] isEqualToString: @"Terminée"])
{
cell.lbl_ImageView.image=[UIImage imageNamed:@"TDEM.png"];
}
if([[[[appDelegate tab_Demandes] objectAtIndex:indexPath.row] demStatut] isEqualToString: @"En coursée"])
{
cell.lbl_ImageView.image=[UIImage imageNamed:@"EDEM.png"];
}
return cell;
}
新编辑:
Demandes.h:
@interface Demande : NSObject {
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
Demandes.m:
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
答案 0 :(得分:0)
上面的代码是正确的。我想知道你是否过度释放你数组中的对象(即在tableView:cellForRowAtIndexPath方法中)。调用removeAllObject时,该方法会释放它删除的每个对象。如果一个对象已经被释放并取消分配,那么您的应用程序将崩溃。
建议的优化:
- 确保nib中的cellIdentifier很好@“Cell”
-replace
cell.lbl_demId.text=[NSString stringWithFormat:@"%@", [[[appDelegate tab_Demandes] objectAtIndex:indexPath.row ] demId]];
通过
Demand* dem = [[[appDelegate tab_Demandes] objectAtIndex:indexPath.row ];
cell.lbl_demId.text=[NSString stringWithFormat:@"%@", [dem demId]];
避免过多地查询数组。
- 避免将模型放在appDelegate中,而是创建一个单例模型对象。这是一个常见的初学者设计错误,因为appDelegate是一个易于定制和访问的对象,可以避免将模型传递给每个对象。