Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-03-2010, 06:08 PM   #1 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 4
maek75 is on a distinguished road
Default Assigning nsarray of custom objects to ivar results in memory leak

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)encodeWithCoderNSCoder *)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)initWithCoderNSCoder *)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];
}
}
maek75 is offline   Reply With Quote
Old 11-03-2010, 06:49 PM   #2 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 327
thomashw is on a distinguished road
Default

Are you releasing listContent in dealloc?

Also, please wrap your code in [ CODE ] tags.
thomashw is offline   Reply With Quote
Old 11-04-2010, 01:21 AM   #3 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 4
maek75 is on a distinguished road
Default

Quote:
Originally Posted by thomashw View Post
Are you releasing listContent in dealloc?

Also, please wrap your code in [ CODE ] tags.
Yes. Then I get the leak when returning. If not released, the app craches when returning.
maek75 is offline   Reply With Quote
Old 11-04-2010, 04:14 AM   #4 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 4
maek75 is on a distinguished road
Default

Quote:
Originally Posted by maek75 View Post
Yes. Then I get the leak when returning. If not released, the app craches when returning.

Just a note; I've tried different ways of allocating the listContent array (but its not shown in the example code). For example:
Code:
self.listContent2 = [[NSKeyedUnarchiver unarchiveObjectWithData:calculationData] copy];
maek75 is offline   Reply With Quote
Old 11-04-2010, 04:26 AM   #5 (permalink)
Registered Member
 
Join Date: Jun 2010
Location: Last spotted in MD.
Posts: 17
LazyCoder is on a distinguished road
Default

Quote:
Originally Posted by maek75 View Post
Just a note; I've tried different ways of allocating the listContent array (but its not shown in the example code). For example:
Code:
self.listContent2 = [[NSKeyedUnarchiver unarchiveObjectWithData:calculationData] copy];
Are you sure you removed that line?
It is leaking, if you do not release your object somewhere else.
LazyCoder is offline   Reply With Quote
Old 11-04-2010, 04:33 AM   #6 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 4
maek75 is on a distinguished road
Default

Quote:
Originally Posted by LazyCoder View Post
Are you sure you removed that line?
It is leaking, if you do not release your object somewhere else.
It should have been:

Code:
self.listContent = [[NSKeyedUnarchiver unarchiveObjectWithData:calculationData] copy];
and that listcontent should have been released in dealloc. But I get a leakage there. How to solve? tried different ways without any luck!
maek75 is offline   Reply With Quote
Old 11-04-2010, 05:01 AM   #7 (permalink)
Registered Member
 
Join Date: Jun 2010
Location: Last spotted in MD.
Posts: 17
LazyCoder is on a distinguished road
Default

Quote:
Originally Posted by maek75 View Post
It should have been:

Code:
self.listContent = [[NSKeyedUnarchiver unarchiveObjectWithData:calculationData] copy];
and that listcontent should have been released in dealloc. But I get a leakage there. How to solve? tried different ways without any luck!
If you copy an object, you own it, and you have to release it.
How you keep track of tht object in your instance is on another page, depends on the declaration of your property.

That line of code is leaking.
LazyCoder is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone sdk, memory crash, nscoding, nsuserdefaults

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 353
12 members and 341 guests
7twenty7, chiataytuday, condor304, Creativ, Domele, dreamdash3, laureix68, LEARN2MAKE, mistergreen2011, mottdog, palme2elie, Paul Slocum
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,660
Threads: 94,119
Posts: 402,896
Top Poster: BrianSlick (7,990)
Welcome to our newest member, laureix68
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:25 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0