OptimusCrime
OptimusCrime

Reputation: 14863

Basic memory-management in objective-c (ios)

I am pretty new to Objective-C and iOS-development, and I am currently trying to grasp how to do memory-management. My app in non-ARC btw.


This object is not declared anywhere in the code (not .h or anything) other than the line belove. Do I need to release/dealloc this object in any way to clear the space for it when I am done using it, or is this done automatically?

NSMutableURLRequest *restRequest = [[NSMutableURLRequest alloc] init];

The same goes for this one. Not sure if this is the same question, but here I don't use the words alloc & init before using it. Does that make any difference?

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

In this case, I am defining the object in the .h-file as well as retaining it. Does this mean that the variable will always be in memory (when initialized once obsly) unless I release/dealloc it? I guess if that is the case, that is something I should do in views when the view is unloaded?

@interface Storeage : NSObject {
    NSString *deviceToken;
}
@property (nonatomic, retain) NSString *deviceToken;

In the .m-file I will alloc and use this object like in the first or second case (does not seems to make any difference).

Please bear with me if this question is stupid. I am used to low level Java-programming with GC.

Upvotes: 1

Views: 3296

Answers (1)

user529758
user529758

Reputation:

Do I need to release/dealloc this object in any way to clear the space for it when I am done using it, or is this done automatically?

Since you are not using ARC, you need to manually send it a release message in order to dispose of its ownership. (Good piece of advice: don't think in terms of "freeing memory". Reference counting means that you increase and decrease reference counts, you get to own and cease to own objects, the deallocation of an object upon having lost all its references is done automatically. In other words, release does not necessarily mean deallocation.)

The same goes for this one. Not sure if this is the same question, but here I don't use the words alloc & init before using it. Does that make any difference?

It does. You only own objects that you create using alloc, new, copy, mutableCopy or reference using retain. You do neither one here, so you don't have to worry about releasing it either. (Technically, it's an autoreleased instance that will be returned, the run loop will take care of it.)

In the .m-file I will alloc and use this object like in the first or second case (does not seems to make any difference).

Make the difference between instance variables and properties. A property setter method, if declared as retain or strong, will retain (increase the reference count of) your object. But that's true only if you use the accessor method, and not when you access the instance variable directly. If you wrote this:

variable = [[SomeObject alloc] init];

then you need to release it just like you would do with any other (local) variable:

[variable release];

If you use the accessor method to set it:

self.variable = [[[SomeObject alloc] init] autorelease];

then you have to use autorelease when creating it (else it will have a reference count of 2 and you'll leak memory).

In both cases, you can also use self.variable = nil; to relinquish ownership. This only works for properties.


All this radically changes with the introduction of ARC, which I don't explain here for three reasons:

  1. I'm not an ARC expert by any means;

  2. I'd like to encourage you to learn MRC (which you seem to have the intention to) perfectly before trying ARC;

  3. It was not the question.

Upvotes: 6

Related Questions