Hi, I store an NSArray of Calculators using NSUserDefault and NSKeyedArchiver. The content is stored fine and I can iterate the content without any memory leakage or app crach.
When the I navigate from the mainViewController to the subview (handled by the CalculatorListViewController) the method reloadTable is called and the array of calculators is unarchived and set to my listContent ivar in the controller. The contents of the array is displayed correctly in the tableview. However, when returning back to the mainView the app either crates or a memory leak is detected using the performance tools in xcode.
I believe the Calculator object is ok, and - as said - I can from within the subviewcontroller iterate the array of calculators without problem. The leakage always occurs after setting the array to the ivar (self.listcontent = …). I've tried retain and lots of stuff, but nothing seems to work.
All help is very appreciated:-)
Below, you find the classes….
Regards Martin
//Calculator.h ---------------
@interface Calculator : NSObject<NSCoding> {
NSNumber *width;
NSNumber *height;
NSNumber *numberOfPages;
NSNumber *weight;
NSNumber *result;
}
@property(nonatomic, retain) NSNumber *width;
@property(nonatomic, retain) NSNumber *height;
@property(nonatomic, retain) NSNumber *numberOfPages;
@property(nonatomic, retain) NSNumber *weight;
@property(nonatomic, retain) NSNumber *result;
//Calculator.m --------------
@implementation Calculator
@synthesize width, height, numberOfPages, weight, result;
#pragma mark -
#pragma mark NSCoding Methods
/* This code has been added to support encoding and decoding my objecst */
-(void)encodeWithCoder

NSCoder *)encoder{
//Encode the properties of the object
[encoder encodeObject:self.width forKey:@"width"];
[encoder encodeObject:self.height forKey:@"height"];
[encoder encodeObject:self.numberOfPages forKey:@"numberOfPages"];
[encoder encodeObject:self.weight forKey:@"weight"];
[encoder encodeObject:self.result forKey:@"result"];
}
-(id)initWithCoder

NSCoder *)decoder{
self = [super init];
if( self != nil ){
//decode the properties
self.width = [decoder decodeObjectForKey:@"width"];
self.height = [decoder decodeObjectForKey:@"height"];
self.numberOfPages = [decoder decodeObjectForKey:@"numberOfPages"];
self.weight = [decoder decodeObjectForKey:@"weight"];
self.result = [decoder decodeObjectForKey:@"result"];
}
return self;
}
-(void)dealloc {
[height release];
[width release];
[numberOfPages release];
[weight release];
[result release];
[super dealloc];
}
----------
Now, when clicking a button on my mainViewcontroller, a subview is presenting a tableview. This subview is handled by the CalculatorListViewController.
The subview is loaded using the code below:
CalculatorListViewController *viewController = [[CalculatorListViewController alloc] initWithNibName:@"CalculatorListViewController" bundle:nil];
[viewController reloadTable];
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
//CalculatorListViewController.h ---------------------
@interface CalculatorListViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>{
NSArray *listContent;
NSArray *listContent2;
}
- (void) reloadTable;
@property (nonatomic, retain) NSArray *listContent;
//CalculatorListViewController.m ---------------------
@implementation CalculatorListViewController
@synthesize listContent;
- (void) reloadTable{
NSData *calculationData = [[NSUserDefaults standardUserDefaults] objectForKey:@"calculatorArray"];
if (calculationData != nil){
self.listContent = [NSKeyedUnarchiver unarchiveObjectWithData:calculationData];
}
}