Why NSString variable needs to be retained? - iphone

I have the following code in my .h file:
#interface Utils : NSObject {
NSString *dPath;
}
#property(nonatomic, retain) NSString *dPath;
And in my .m file:
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
dPath = [[documentPaths objectAtIndex:0] stringByAppendingPathComponent:kDatabaseName];
[dPath retain];
Why do I have to retain dPath if it's already defined as (nonatomic, retain)?
If I don't add the [dPath retain]; I get some strange, random errors and the application crashes when using this variable in other functions. I guess that's because of some autorelease somehere but I don't have any.
So, what is the (nonatomic, retain) doing anyway? Is it really necessary the [dPath retain]; or am I just hiding something else with that?

Because the code isn't calling the dPath property's setter method, it's just setting the instance variable dPath directly:
dPath = [[documentPaths objectAtIndex:0] stringByAppendingPathComponent:kDatabaseName];
[dPath retain];
So it has to be retained manually.
You will be able to (in fact you need to) omit the retain call if the property setter was used like this (notice the self.):
self.dPath = [[documentPaths objectAtIndex:0] stringByAppendingPathComponent:kDatabaseName];
or like this (notice the setDPath:):
[self setDPath:[[documentPaths objectAtIndex:0] stringByAppendingPathComponent:kDatabaseName]];
The setter retains the NSString for you so you don't have to do it yourself.
A nice little practice to follow in order to avoid the confusion, is to affix an underscore to your ivar name to indicate it's an ivar:
NSString *dPath_;
Then synthesize your property like this, to associate it with your differently-named ivar:
// self.dPath is the property, dPath_ is the ivar
#synthesize dPath = dPath_;
Then modify your dealloc method, as well as any other code that directly references the instance var, to use the affixed name instead:
- (void)dealloc {
[dPath_ release];
[super dealloc];
}

try setting and getting it with
self.dPath

If you want to call the property setter method, which will invoke the retain, then you want to write:
self.dPath = ...
Jamming stuff into a variable with:
dPath = ...
completely ignores the properties of this instance variable. Which is why you ended up needing to do the retain manually.

Related

Getting Properties Set in Constructor

I am new to Objective-C. I am trying to get a property set in the constructor, but am getting an EXC_BAD_ACCESS error. Here is my constructor:
- (id) init {
self = [super init];
if (self != nil) {
appFolderPath = [[NSBundle mainBundle] resourcePath];
fileManager = [NSFileManager defaultManager];
mediaArray = [fileManager directoryContentsAtPath: [appFolderPath stringByAppendingString:#"/Media/Silly"]];
mediaIndex = 0;
}
return self;
}
Here are my properties:
#property (retain) NSFileManager* fileManager;
#property (retain) NSString* appFolderPath;
#property int mediaIndex;
#property (retain) NSArray* mediaArray;
Any ideas?
You have the retain keyword on your properties, which is good. But, it doesn't matter because you are not actually using them. You are accessing the ivars directly, bypassing the getter method that the Objective-c compiler generated for you.
To contrast between and ivar and a property, see this code:
MyInterface.h
#interface MyInterface : NSObject {
#private
NSFileManager * fileManager; // This is an instance variable, or 'ivar'
}
#property (retain) NSFileManager * fileManager; // This is the declaration of a property
MyInterface.m
#implementation MyInterface {
#synthesize fileManager;
// Calling this causes the Objective-C compiler to generate the following
// methods for you:
// -(NSFileManager *) getFileManager ...
// and - (void) setFileManager: (NSFileManager *) val ...
}
To use the ivar from your class you would simply reference its name, in your example you did that with the line:
fileManager = [NSFileManager defaultManager];
Since the auto-released instance returned by the method you called isn't being retained, you get an EXEC_BAD_ACCESS exception later on in your program. To use the property you need to preface it with the owning object reference:
self.fileManager = [NSFileManager defaultManager];
This ensures that your ivar is set and retained.
EDIT
Now, to really appreciate the difference between an instance variable and a property, you could have declared your interface as:
#interface MyInterface : NSObject {
#private
NSFileManager * _fileManager;
}
#property (retain) NSFileManager * fileManager;
And in your .m you would have:
#synthesize fileManager = _fileManager.
Now when you tried to do fileManager = [NSFileManager defaultManager]; your code would not compile, because there is no ivar called fileManager in your class. It's a useful coding style to avoid common mistakes.
#Perception already explained the ins and outs of why this doesn't work. Here's how to rectify this. Since you're not using the synthesized setters which would send the retain message, you're forced to handle it yourself when directly accessing the instance variables. Also note that objects of type NSString should be copied instead of retained, so the declaration of appFolderPath should actually be #property (nonatomic, copy) NSString *appFolderPath;. Taking all of this into account, your -init should look something like this.
- (id)init
{
self = [super init];
if (self != nil)
{
appFolderPath = [[NSBundle mainBundle] resourcePath] copy];
fileManager = [NSFileManager defaultManager] retain];
mediaArray = [fileManager directoryContentsAtPath:[appFolderPath stringByAppendingString:#"/Media/Silly"]] retain];
mediaIndex = 0;
}
return self;
}
You aren't assigning to properties, you're assigning to instance variables. If you want to use the properties, you need to prefix the names with self., as in self.fileManager.
The effect is that the objects aren't retained, and probably get deallocated later on. When you then later try to access the properties (or their instance variables) the objects are already gone. Sometimes there's a different object in their place, sometimes it's garbage. And that's when you get the crash.
I don't want to step on any toes - #Perception has provided a thorough explanation and then #Mark gave the actual solution.
Here's the quick version:
This
#property (nonatomic, copy) NSString* appFolderPath;
and matching
#synthesize appFolderPath = _appFolderPath;
mean that the compiler generates the following methods
- (void)setAppFolderPath(NSString *)appFolderPath;
- (NSString *)appFolderPath;
These methods will take care of the memory management of retaining/copying/referencing an object on assignment depending on which option you choose retain/copy/assign. This memory management will only come into effect if you use
[self setAppFolderPath:#"My Folder Path"];
or
self.appFolderPath = #"My Folder Path"; // which compiles to the previous call
Now this is all well and good for general use in your class but sometimes it is recommended to not use getters and setters in case they cause side effects. Two places where you normally want to avoid getters and setters are in init methods and dealloc.
Therefore as you was in the init method you should access the ivar directly without using the getters/setters. This means that you will need to perform the memory management yourself. e.g.
- (id)init
{
self = [super init];
if (self) {
_appFolderPath = [[[NSBundle mainBundle] resourcePath] copy];
}
return self;
}
In the dealloc method you would access the ivar directly again like this
- (void)dealloc
{
[_appFolderPath release];
[super dealloc];
}
As #Mark pointed out you generally copy strings so that they can't be changed underneath you.
Additional notes on examples.
When I called #synthesize appFolderPath = _appFolderPath; It creates the getters and setters using the name appFolderPath but the ivar these methods effect is actually called _appFolderPath. This is good practice as you will always be sure when you are accessing the ivar directly or through a getter/setter because just referencing appFolderPath in your code will not compile.

Objective C: retain vs alloc

I have a singleton class with this code:
manager.h
#interface Manager : NSObject {
NSString *jobsLimit;
NSMutableDictionary *jobTitles;
}
#property (nonatomic, retain) NSString *jobsLimit;
#property (nonatomic, assign) NSMutableDictionary *jobTitles;
#implementation Manager
#synthesize jobsLimit;
#synthesize jobTitles;
+ (id)sharedManager {
#synchronized(self) {
if(shared == nil)
shared = [[super allocWithZone:NULL] init];
}
return shared;
}
- (id)init {
if (self = [super init]) {
jobsLimit = [[NSString alloc] initWithString:#"50"];
jobTitles = [[NSMutableDictionary alloc] init];
}
return self;
}
Then in the code i'm assigning these variables like this:
self.jobsLimit = [NSString stringWithFormat:#"%d", progressAsInt];
[self.jobTitles addEntriesFromDictionary:anotherDictionary];
- (void)dealloc {
[super dealloc];
[jobsLimit release];
[jobTitles release];
}
Now my question is this code correct? Is the assignment correct?
I'm very confused with when to use alloc and/or retain. Do I need to use alloc if my property is already retained? and If I use alloc should my property be assign?
What will be the reference count now for these variables and will they be dealloc'd/under-dealloc'd when the dealloc is called?
Also for singleton classes do I need to initialize my ivars as in the init method above or I do not have to.
I'd appreciate if someone can help me clear this confusion out and thanks in advance.
Regards,
Your code actually looks correct, but perhaps some explanation is in order, since it sounds like you're a little unsure.
When you assign to a property that has retain semantics using the "." syntax, the accessor method calls retain. The "." syntax is just shorthand for invoking the accessor method, so
self.jobsLimit = [NSString stringWithFormat:#"%d", progressAsInt];
is exactly the same as
[self setJobsLimit:[NSString stringWithFormat:#"%d", progressAsInt]];
That works out to:
create an (autoreleased) string with a numeric value
retain the string (you now own it) and assign it to jobsLimit
If, on the other hand, you assign to the iVar directly (not using the "."-accessor), the setter method is not called. For example:
jobsLimit = [[NSString alloc] initWithString:#"50"];
That is:
allocate a string (you own it), with value "50"
assign it to jobsLimit
Either way, you now own the string referred to by jobsLimit, and are responsible for eventually releasing it (e.g., in your dealloc method).
I guess you need a lot of this : IOS Memory Management
and a bit of : What should my Objective-C singleton look like?
good lectures !

should variable be retained or not? iphone-sdk

in the following piece of code I got from a book.
The NSString *pPath which is defined in the class as an instance variable.
#interface MainViewController : UIViewController {
NSString *pPath;
}
In the implementation after being set it is being retained. I assume that with the assignment the object is automatically retained (because it is an NSString) and there is no need to additionally retain it.
- (void) initPrefsFilePath {
NSString *documentsDirectory =
[NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
pPath = [documentsDirectory stringByAppendingPathComponent:
#"flippingprefs.plist"];
[pPath retain];
}
Yes, you need to retain your pPath variable if you obtain it this way. However it is not the end of the story - you also need to release its previous value otherwise it will just leak.
To make things easier you can use objective-c properties that allow you to automatically generate setter/getter methods with desired memory management behavior:
// header
#interface MainViewController : UIViewController {
NSString *pPath;
}
#property (nonatomic, retain) NSString* pPath;
// implementation
#synthesize pPath;
- (void) initPrefsFilePath {
NSString *documentsDirectory =
[NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
self.pPath = [documentsDirectory stringByAppendingPathComponent:
#"flippingprefs.plist"];
}
Here in self.pPath=... line automatically generated setter method will get called which:
Will release previously set pPath value
Will assign new value to pPath and retain it
You will also need to release your pPath variable in dealloc method:
-(void) dealloc{
[pPath release];
//or
self.pPath = nil;
[super dealloc];
}

How am I leaking memory?

I have a table view that when loading creates a person object
Person.h
#import <UIKit/UIKit.h>
#import "TwitterHelper.h"
#interface Person : NSObject {
NSDictionary *userInfo;
NSURL *image;
NSString *userName;
NSString *displayName;
NSArray *updates;
}
/*
#property (retain) NSString *userName;
#property (retain) NSString *displayName;
#property (retain) NSDictionary *userInfo;
*/
#property (nonatomic, copy) NSURL *image;
#property (retain) NSArray *updates;
- (id)initWithUserName:userName;
#end
Person.m
#import "Person.h"
#implementation Person
/*
#synthesize userName;
#synthesize displayName;
#synthesize userInfo;
*/
#synthesize image;
#synthesize updates;
- (id)initWithUserName:(NSString *)user{
userName = user;
userInfo = [TwitterHelper fetchInfoForUsername:user];
displayName = [userInfo valueForKey:#"name"];
image = [NSURL URLWithString:[userInfo valueForKey:#"profile_image_url"]];
updates = [TwitterHelper fetchTimelineForUsername:userName];
return self;
}
- (void)dealloc
{
/*
[userName release];
[displayName release];
[updates release];
[userInfo release];
[image release];
*/
[super dealloc];
}
#end
Inside my UITableView method cellAtRowForIndexPath I am creating each person object and assigning the image property like so...
Person *person = [[Person alloc] initWithUserName:userName];
NSData *data = [[NSData alloc] initWithContentsOfURL:person.image];
[data release];
When I run this in Instruments it highlights the NSData *data... row saying that is where the leak is.
Why is it leaking there?
First, you need to understand the difference between instance variables and properties and getter/setters.
instance variables (ivars) are variables stored in
your object. You access an ivar from within a method simply by naming it (eg "userName").
properties define an
interface to your object, allowing
information to be read and/or written
to your object.
getters/setters implement that interface and may use an ivar as backing storage
You access a property by using a getter/setter, either explicitly (eg [self userName]) or (equivalently) using dot syntax self.userName. Note that these two notations are exactly identical. You declare a property (ie, you declare an interface to your object) using #property in the interface of your object, something like:
#property (copy) NSString* userName;
This declartion is essentially equivalent to typing:
- (NSString*) userName;
- (void) setUserName: (NSString*) theUserName;
You implement a property, either by using #synthesize (which simply tells the compiler to write the getter/setter for you) or by implementing it yourself (ie, you write methods implementation for userName and setUserName). There is also a rarely used third option, #dynamic, which tells the compiler you will handle the methods at run time, essentially just silincing the warning you would otherwise get.
Next, you need to read and understand the memory management rules. Its only 9 short paragraphs, go read it now, I'll wait. Done? good.
Further, you need to know that you should not use getters/setters in either the init or dealloc routines.
So your init routine should look something like this:
- (id)initWithUserName:(NSString *)user{
userName = [user copy];
userInfo = [[TwitterHelper fetchInfoForUsername:user] retain];
displayName = [[userInfo valueForKey:#"name"] copy];
image = [[NSURL URLWithString:[userInfo valueForKey:#"profile_image_url"]] copy];
updates = [[TwitterHelper fetchTimelineForUsername:userName] retain];
return self;
}
Note that you take ownership of each value you store in an ivar with retain or copy. Generally, you use copy for NSString to convert an NSMutableStrings into NSStrings you own, rather than retain which would leave you holding a reference to a possibly mutable string. The same issue applies to NSArray/NSDictionary, but we will assume TwitterHelper intends to hand off the fetched data.
Your dealloc will have to release the various ivars:
- (void)dealloc
{
[userName release];
[displayName release];
[updates release];
[userInfo release];
[image release];
[super dealloc];
}
Anywhere else in your code you would use self.userName to access or change the properties, rather than access the ivars directly.
Note that you might consider not storing the displayName (and similarly image) at all, but simply implement a property getter that retrieves it from userInfo. To do this, delete the displayName ivar, change the property to:
#property (readonly) NSString *displayName;
remove the #synthesize displayName, and add a manual getter:
- (NSString*) displayName
{
return [userInfo valueForKey:#"name"];
}
and remove the release in dealloc.
Note that you do not need to retain/release the value in displayName - you return a value that the receiver does not own and it is up to them to copy/retain it if they want to keep it.
If you choose to create a property, you should use:
self.image = [NSURL URLWithString:[userInfo valueForKey:#"profile_image_url"]];
in your init message and not
image = [NSURL URLWithString:[userInfo valueForKey:#"profile_image_url"]];
Setting the value without the self prefix will not call the copy or retain message, and will create a memory problem (not necessarily a leak).
This might be what Instruments is pointing you to.
(This obviously applies to all properties!)
Alternatively, if you don't want to use the accessor, then retain or copy the value retrieved, e.g.:
image = [[NSURL URLWithString:[userInfo valueForKey:#"profile_image_url"]] retain];
You are calling alloc on Person but not releasing it. You've leaked your person object.
(in your cell configuration)

iphone assigning instance variables

I just spent the last 3 hours trying to figure out this error. I would like someone to explain it to me so I don't do it again.
I assigned an NSString instance variable without using "self". When the class ("self") released, I received a "bad access" error. I have done this exact same thing in another class with the same variable declarations and do not have this error. Below is my code. I commented out the line that broke and the line below fixes it. But I don't understand why... Notice that there are other instance variables that do not cause this problem. Should I always use the "self" reserved word when assigning instance variables? Please let me know.
declarations
#property (nonatomic, readonly, assign) int IID;
#property (nonatomic, assign) int ProfileIID;
#property (nonatomic, retain) NSDate *NoteDate;
#property (nonatomic, copy) NSString *NoteText;
code snippet
// the default date format is Year-Month-Day
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:kDateFormat];
IID = sqlite3_column_int(selectstmt, 0);
ProfileIID = sqlite3_column_int(selectstmt, 1);
// notice this does not cause a memory error
NoteDate = [[df dateFromString: [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)]] retain];
// the following may be NULL. Checking using the sqlite3_column_text method
const char *columnText = (const char *)sqlite3_column_text(selectstmt, 3);
if(columnText != NULL)
{
// this causes a memory error
//NoteText = [NSString stringWithUTF8String: columnText ];
// this does not cause memory error
self.NoteText = [NSString stringWithUTF8String: columnText ];
}
The reason that
NoteDate = [[df dateFromString: [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)]] retain];
is fine is because you retain the variable. Since you do not allocate the string, but call stringWithUTF8String on NSString, you do not take ownership of the variable, and so the string returned to you is autoreleased. However, since you retain it, this does not cause problems.
If variables are returned autoreleased, then they are released when the autorelease pool is drained, which occurs at the end of each event (see more on autorelease pools). This is no good with an instance variable, because it needs to stick around after the current event.
When you assign the variable by:
NoteText = [NSString stringWithUTF8String: columnText];
Your setter method is not invoked, so the returned string (which, again, is autoreleased) is not retained, and so is released by the autorelease pool at the end of the event.
Calling it as:
self.NoteText = [NSString stringWithUTF8String: columnText];
does retain the string, since the line is another way of writing:
[self setNoteText:[NSString stringWithUTF8String: columnText]];
which invokes your setter method and retains the variable, preventing it from being released at the end of the current event.
Assigning NoteText from within a class method doesn't invoke the synthesized setter method, it sets the instance variable directly. This means your string isn't being retained (or copied, in the case of your setter), and the crash is when trying to release an object which has already been released. Do this:
self.NoteText = [NSString stringWithUTF8String: columnText ];
This will call your setter, and all will be well.
Edit:
Just to be clear, this is true for all ivars.
myVariable = someValue; // Sets the ivar directly.
self.myVariable = someValue; // calls [self setMyVariable].
This confusion between ivar and setter methods is why I never name my setters and my ivars the same.
Apple typically names its ivars starting with an underscore (_), for example NoteText. In my case, I've taken to having a prefix of i for ivars. For example:
NSString* i_name;
#property (nonatomic, copy) NSString* name;
#synthesize name = i_name;
That way you can easily tell the difference between an ivar assignment:
i_name = [[whatever title] retain];
and a setter method call
self.name = [whatever title]; // Equivalent to [self setName:[whatever title]
The setter, since it is defined with copy (or similarly for retain) will take ownership of the variable passed in and release ownership of the old value. The ivar assignment does none of that.
Note also that your propery names should start with a lower case letter or they will not be KVO compliant.