Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cjlesh

macrumors newbie
Original poster
Jan 6, 2009
5
0
I'm still very new to memory management in general, having only programmed in languages where it wasn't really a consideration.

As I was working through an example in the Apress "Beginning Iphone Development" book, I ran into something I can't understand, and I'm hoping someone here can point me in the right direction.

In Chapter 6, page 126, there is the following code:

Code:
...
@synthesize blueViewController;
...
 - (void)viewDidLoad {
       BlueViewController *blueController = [[BlueViewController alloc] initWithNibName:@"BlueView" bundle:nil];
       self.blueViewController = [[BlueViewController alloc] initWithNibName:@"BlueView" bundle:nil];
       [self.view insertSubview:blueController.view atIndex:0];
       [blueController release];
 }
...

OK, I don't want to reproduce the entire project's code here, but in the header file there is:

Code:
@property (retain, nonatomic) BlueViewController *blueViewController;

and in the dealloc method:

Code:
[blueViewController release];

So, I was wondering why I can't do this instead:

Code:
- (void)viewDidLoad {
       self.blueViewController = [[BlueViewController alloc] initWithNibName:@"BlueView" bundle:nil];
       [self.view insertSubview:blueViewController.view atIndex:0];
}

I asked this question on the book's forum, and was told that my code will result in a memory leak, because alloc increased the ref count by 1, and assignment increases the ref count by 1, and I release blueViewController later, but that leaves a ref unreleased.

So, that makes sense to me, except when I run my code through 'Leaks', I'm not finding any.

Which leads me to my questions:
1. Why am I not seeing any leaks with my code?
2. Do I really have to create then release a temp variable every time I want to create an object that is assigned to an instance variable?
3. I tried reading the Apple docs on memory management, and they are over my head. Is there anywhere I can read about memory management in Objective C that won't make my head hurt?

Thanks in advance.
 

tsornin

macrumors newbie
Jul 23, 2002
26
0
So, I was wondering why I can't do this instead:

Code:
- (void)viewDidLoad {
       self.blueViewController = [[BlueViewController alloc] initWithNibName:@"BlueView" bundle:nil];
       [self.view insertSubview:blueViewController.view atIndex:0];
}

I do the above all the time without problems. You should add a [blueViewController release] to your dealloc method.

So, this code:
Code:
BlueViewController *viewController = [[blueViewController alloc] initWithNibName:@"BlueView" bundle:[NSBundle mainBundle]];
self.blueController = viewController;
[viewController release];
NSLog(@"retain count for blue controller: %d", (int)[blueController retainCount]);

is the same as this:
Code:
blueController = [[BlueViewController alloc] initWithNibName:@"BlueView" bundle:[NSBundle mainBundle]];
NSLog(@"retain count for blue controller: %d", (int)[blueController retainCount]);

Both should give you a retain count of 1. Which is what you want.
 

newb16

macrumors regular
Feb 27, 2008
100
0
Code:
 - (void)viewDidLoad {
       BlueViewController *blueController = [[BlueViewController alloc] initWithNibName:@"BlueView" bundle:nil];
       self.blueViewController = [[BlueViewController alloc] initWithNibName:@"BlueView" bundle:nil];
       [self.view insertSubview:blueController.view atIndex:0];
       [blueController release];
 }
...

The code in the book ( at least in the googlebooks ) is different in one line -
Code:
       self.blueViewController = blueController
Else you end up with one controller and insert'ed Subview from different (another) controller.
 

cjlesh

macrumors newbie
Original poster
Jan 6, 2009
5
0
newb16:

You are correct, the code I pasted above is not the code from the book, it is actually what you provided. I should have pasted:

Code:
...
@synthesize blueViewController;
...
 - (void)viewDidLoad {
       BlueViewController *blueController = [[BlueViewController alloc] initWithNibName:@"BlueView" bundle:nil];
       self.blueViewController = blueControler;
       [self.view insertSubview:blueController.view atIndex:0];
       [blueController release];
 }
...

In fact, I've done more reading on this, and seen similar code in Apple's examples, so maybe this is a common idiom? Create a temp variable, allocate a new object to it, hand off the object to your property, then delete the temp variable. I can do it, it just seems needlessly verbose.

So, my original question still stands, does the code change I proposed introduce a memory leak? On the book's forum, the respondent insists that it does, and I can't argue with the reasoning (I don't know enough), but I'm not seeing any leaks when I run my code through 'Leaks'.

Anyway, thanks for the replies, I clearly need to learn more about memory management in Objective-C and Cocoa.

-cjlesh
 

newb16

macrumors regular
Feb 27, 2008
100
0
newb16:

You are correct, the code I pasted above is not the code from the book, it is actually what you provided. I should have pasted:

Code:
...
@synthesize blueViewController;
...
 - (void)viewDidLoad {
       BlueViewController *blueController = [[BlueViewController alloc] initWithNibName:@"BlueView" bundle:nil];
       self.blueViewController = blueControler;
       [self.view insertSubview:blueController.view atIndex:0];
       [blueController release];
 }
...
The difference here is that if you set result [[BlueViewController alloc] ...] to your property, then instead of "insertSubview:blueController.view" you need to write somthing like insertSubview:self.controller.view
where [self controller] ( as I understand properties) is one more selector call, instead of referencing local variable. It's no problem if you don't need it in this scope anymore.


So, my original question still stands, does the code change I proposed introduce a memory leak? On the book's forum, the respondent insists that it does, and I can't argue with the reasoning (I don't know enough), but I'm not seeing any leaks when I run my code through 'Leaks'.

To me - yes, as you first have retain count of 1 after alloc/init and then of 2 after retaining it in your property (retain, nonatomic), and after you release the property it is still 1.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.