iOS: Memory leak in simple MVC model

2019-09-08 16:17发布

问题:

I've build (must be simple...) MVC model, but I still have memory leak when pushing back button.

Model class: .h

@interface Nominal : NSObject {

   int nominalID;
   NSString *nominal;
   NSString *nominalImg;
   NSString *nominalName;
}
@property(nonatomic)int nominalID;
@property(nonatomic,retain)NSString *nominal;
@property(nonatomic,retain)NSString *nominalImg;
@property(nonatomic,retain)NSString *nominalName;
@end

.m

@implementation Nominal
@synthesize nominal,nominalID,nominalImg,nominalName;
-(void)dealloc
 {
   [self.nominal release];
   [self.nominalImg release];
   [self.nominalName release];
 }
@end

I do release the strings as well.

In my view class I populate it so:

.h

@interface Nominals : UIViewController {
     ...
     NSMutableArray *nominalsArr;
     ...
}
@property(retain,nonatomic)NSMutableArray *nominalsArr;

.m

 - (void)viewWillAppear:(BOOL)animated 
{
 [[self navigationController]setToolbarHidden:YES animated:YES];
   DBAccess *dbAccsess=[[DBAccess alloc]init];
   self.nominalsArr=[dbAccsess returnNominals:subCountryID];
   [dbAccsess closeDataBase];
   [dbAccsess release];
   [super viewWillAppear:animated];
}
- (void)dealloc
{
  [nominalsArr release];
  [self.navigationController release];
  [super dealloc];
}

Looks like I do release the whole bundle of holy things, but when I push pack button from this view to previous, the memory leak pops up:

What I'm doing wrong?

You help is utterly appreciated.

回答1:

You've forgotten a [super dealloc] in [Nominal -dealloc]. Also, don't call [self.navigationController release] as that property is already handled by the superclass (UIViewController).