#properties - Memory Management - iphone

would this code be ok concerning memory management?
#property (nonatomic, retain) id object;
...
id anObject = [[Object alloc] init];
self.object = anObject;
id otherObject = [[Object alloc] init];
self.object = otherObject;
Thanks for your answers,
Christian

No. As the property descriptor indicates, it will retain the object when it's assigned. So, as you're allocating it, you'll have two retains in your object at the moment of assign it to self.object. So, you have to release it:
#property (nonatomic, retain) id object;
...
id anObject = [[Object alloc] init];
self.object = anObject;
[anObject release];
id otherObject = [[Object alloc] init];
self.object = otherObject;
[otherObject release];
...
at dealloc:
self.object = nil;
Good luck!

Related

Dealloc of custom object

I have this Class in my project :
#interface VideoItem : NSObject <NSCoding> {
NSString *name;
NSString *artist;
int seconds;
}
#property (nonatomic, retain) NSString *name;
#property (nonatomic, retain) NSString *imgUrl;
#property (nonatomic, retain) NSString *artist;
#end
And this is how i create this object:
VideoItem *item = [[VideoItem alloc] init];
item.name = name;
item.imgUrl = imgLink;
item.artist = artist;
And this is the dealloc:
- (void)dealloc{
[name release];
[imgUrl release];
[artist release];
[super dealloc];
}
And i want to know if this dealoc is ok with the NON-ARC? did i need to do something else because this NSString are with Property?
Edit
And if the VideoItem object was create with:
VideoItem *item = [[VideoItem alloc] init];
item.name = [NSString alloc]initWithFormat#"%#",name];
item.imgUrl = [NSString alloc]initWithFormat#"%#",imgLink];
item.artist = [NSString alloc]initWithFormat#"%#",artist];
Did in this case the dealloc is still ok? or i need to change something?
Everything looks ok, you are releasing all the #properties of your object. I would probably as well point them to nil, just to make sure, that if one of those properties is called, it will be nilled and not have a garbage value, like so:
- (void)dealloc{
[name release], name = nil;
[imgUrl release], imgUrl = nil;
[artist release], artist = nil;
[super dealloc];
}
Another thing, no related, it would be cleaner, if you would create your own init, so you can pass the properties values, when you actually create the object, like so:
-initWithName:(NSString *)name withImgURL:(NSString *)imgURL withArtist:(NSString *)artist;
Your edit:
item.name = [NSString alloc]initWithFormat#"%#",name];
item.imgUrl = [NSString alloc]initWithFormat#"%#",imgLink];
item.artist = [NSString alloc]initWithFormat#"%#",artist];
Only based on this, it will create a leak, so you should be careful. To fix this:
item.name = [[NSString alloc]initWithFormat#"%#",name] autorelease];
item.imgUrl = [[NSString alloc]initWithFormat#"%#",imgLink] autorelease];
item.artist = [[NSString alloc]initWithFormat#"%#",artist] autorelease];
If you don't have ARC enabled than your destructor is correct. You are releasing all the properties that are retained and calling super, which is all you need.

Leaky Custom Object for storing data from a plist

I have made a very simple custom object pictureData.
Here is the .h file
#import <Foundation/Foundation.h>
#interface pictureData : NSObject {
NSString *fileName;
NSString *photographer;
NSString *title;
NSString *license;
}
#property (nonatomic, retain) NSString *fileName;
#property (nonatomic, retain) NSString *photographer;
#property (nonatomic, retain) NSString *title;
#property (nonatomic, retain) NSString *license;
+(pictureData*)picDataWith:(NSDictionary*)dictionary;
#end
The .m file
#import "pictureData.h"
#implementation pictureData
#synthesize fileName;
#synthesize photographer;
#synthesize title;
#synthesize license;
+ (pictureData*)picDataWith:(NSDictionary *)dictionary {
pictureData *tmp = [[[pictureData alloc] init] autorelease];
tmp.fileName = [dictionary objectForKey:#"fileName"];
tmp.photographer = [dictionary objectForKey:#"photographer"];
tmp.title = [dictionary objectForKey:#"title"];
tmp.license = [dictionary objectForKey:#"license"];
return tmp;
}
-(void)dealloc {
[fileName release];
[photographer release];
[title release];
[license release];
}
#end
I then set up these objects in an array, like so:
NSString *path = [[NSBundle mainBundle] pathForResource:#"pictureLicenses" ofType:#"plist"];
NSArray *tmpDataSource = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *tmp = [[NSMutableArray alloc] init];
self.dataSource = tmp;
[tmp release];
for (NSDictionary *dict in tmpDataSource) {
pictureData *pic = [pictureData picDataWith:dict];
NSLog(#"%#", pic.title);
[self.dataSource addObject:pic];
}
Everything works smashingly. I have a table view which loads the proper picture images, and information, no problem. Upon running Instruments for leaks, I see that my pictureData object is leaks with every allocation.
I would assume that with having my object autoreleased I would not have to worry about manually allocating and deallocating them.
Perhaps is my issue that I use autorelease, which the autoReleasePool keeps a retain count of +1 and then when I add a pictureData object to my array, that also retains it? Thank you all for your time!
edit: Don't forget to call super! Thank you Sam!
Change dealloc to:
-(void)dealloc {
[fileName release];
[photographer release];
[title release];
[license release];
[super dealloc];
}
(call [super dealloc])
In your function, change the return value to include autorelease, like
+ (pictureData*)picDataWith:(NSDictionary *)dictionary
{
...
...
return [tmp autorelease];
}
When you add pictureData object to dataSource, you increase the retain count, so you should autorelease it while returning.
Hope it helps.

cannot pass the obj-c class obj to another view

I have 3 classes
First -> MainViewController:
#interface MainViewController : UIViewController {
UtilityBadah *utility;
}
#property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
- (IBAction)option;
#end
#implementation MainViewController
#synthesize managedObjectContext = _managedObjectContext;
(IBAction)option{
UtilityBadah *util = [[UtilityBadah alloc] initWithContext:_managedObjectContext];
OptionController *ovc = [[OptionController alloc] init];
ovc.util = util;
ovc.managedObjectContext = _managedObjectContext;
[self.navigationController pushViewController:ovc animated:YES];
[util release];
[ovc release];
}
#end
Second -> UtilityBadah:
#interface UtilityBadah : NSObject {
NSManagedObjectContext *managedObjectContext;
NSString *kitab;
NSString *lagu;
NSString *font;
NSString *sizefont;
}
#property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, retain) NSString *kitab;
#property (nonatomic, retain) NSString *lagu;
#property (nonatomic, retain) NSString *font;
#property (nonatomic, retain) NSString *sizefont;
(id) initWithContext: (NSManagedObjectContext *) context;
#end
#implementation UtilityBadah
#synthesize managedObjectContext;
#synthesize kitab;
#synthesize lagu;
#synthesize font;
#synthesize sizefont;
-(id) initWithContext: (NSManagedObjectContext *) context {
NSError *err;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *enDesc = [NSEntityDescription entityForName:#"OptionDB" inManagedObjectContext:context];
[request setEntity:enDesc];
NSArray *arrData = [context executeFetchRequest:request error:&err];
for (OptionDB *data in arrData) {
lagu = data.lagu;
kitab = data.kitab;
font = data.font;
sizefont = data.sizefont;
}
return self;
}
Thrid -> OptionController:
#interface OptionController : UIViewController{
NSManagedObjectContext *managedObjectContext;
UtilityBadah *util;
}
#property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, retain) UtilityBadah *util;
#end
(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
bla..bla..
NSLog(#"value is %#",self.util.kitab);
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
}
i wonder why this piece of code:
NSLog(#"value is %#",self.util.kitab);
cause an error "Receiced signa: "EXC_BAD_ACCESS" ?
i wonder ther somthing wrong the way i passed the UtilityBadah object from MainViewController to my OptionController.
Many thanks for any answer.
P.S what is wrong with my xcode 4.2 error information, why it always show "Program received signal: "EXC BAD ACCESS"? cant it be informative?
i think your init method is wrong, try assigning self to [super init] at the start, and doing all your set up after checking that self != nil. this is the basic form of an init:
- (id)init
{
self = [super init];
if (self)
//do setup
return self;
}
also, you are using a subclass of NSObject and associating a managed object context to it, you should look into core data in more detail, you should be using NSManagedObjects
The property name in the OptionController is util not utility
If you want to access the utility property in the UtilityBadah class you will have to do:
self.util.utility
In the UtilityBadah class, initWithContext method, change the for loop as following
for (OptionDB *data in arrData) {
self.lagu = data.lagu;
self.kitab = data.kitab;
self.font = data.font;
self.sizefont = data.sizefont;
}
In your case, the value is not retained since you have assigned without accessing through the property.
You need to allocate those objects first. Here's how your initWithContext method should look like:
-(id) initWithContext: (NSManagedObjectContext *) context {
self = [super init];
if (self) {
NSError *err;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *enDesc = [NSEntityDescription entityForName:#"OptionDB" inManagedObjectContext:context];
[request setEntity:enDesc];
NSArray *arrData = [context executeFetchRequest:request error:&err];
for (OptionDB *data in arrData) {
self.lagu = [NSString stringWithString:data.lagu];
self.kitab = [NSString stringWithString:data.kitab];
self.font = [NSString stringWithString:data.font];
self.sizefont = [NSString stringWithString:data.sizefont];
}
}
return self;
}

NSMutableArray in Singleton object

I want to build a shared object that all the classes will be able to access to it.
i want that in this object will be NSMutableArray .
this is how i call this object
+(id)sharedManager{
#synchronized(self) {
if (sharedManager == nil){
sharedManager = [[self alloc] init];
array = [[NSMutableArray alloc] init];
}
}
return sharedManager;
}
and this is how i define the NSMutableArray :
#property (nonatomic,retain) NSMutableArray *array;
the problem is that after i create this NSMutableArray in the sharedManager method, every time i try to access the array is equal to Nil.
You're attempting to set an instance variable from a class method. Instead, you should create array in your -init method in the singleton. That way when you message, sharedManager = [[self alloc] init];, the array will be configured for that shared instance.
- (id)init
{
self = [super init];
if (!self)
return nil;
array = [[NSMutableArray alloc] init];
return self;
}

Problem with a NSMutableArray Leak

I thought I knew how to deal with memory leaks and arrays, but then this pops up. I can't figure pout why this is leaking:
// MyViewController.h
NSMutableArray *myMutableArray;
#property (nonatomic, retain) NSMutableArray *myMutableArray;
// MyViewController.m
#synthesize myMutableArray;
- (void) viewDidLoad {
if (self.myMutableArray == nil) {
self.myMutableArray = [[NSMutableArray alloc] init];
}
. . .
for (NSUInteger i = 0; i < someCount; ++i) {
[self.myMutableArray addObject:[NSString stringWithFormat: #"%#",myString]];
}
}
- (void)viewDidUnload {
self.myMutableArray = nil
}
- (void)dealloc {
[myMutableArray release];
}
Your problem is here:
if (self.myMutableArray == nil) {
self.myMutableArray = [[NSMutableArray alloc] init];
}
It should be:
if (myMutableArray == nil) {
self.myMutableArray = [[[NSMutableArray alloc] init] autorelease];
}
Or:
if (myMutableArray == nil) {
myMutableArray = [[NSMutableArray alloc] init];
}
Explanation:
Since you are using retain as a property mutator attribute, the object will be retained when it is passed to the property setter, therefore you have a leak when you retain an object you already have ownership of.
The solution to this is to either a) Pass an autorelease-d object to the property setter or b) Assign the ivar directly to the alloc-ed object.
You're allocating a new array, then setting it to a retain property. Change that line to
self.myMutableArray = [NSMutableArray array];
self.myMutableArray = [[NSMutableArray alloc] init];
should be
self.myMutableArray = [[[NSMutableArray alloc] init] autorelease];
because myMutableArray is a retained property.