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:
OK, I don't want to reproduce the entire project's code here, but in the header file there is:
and in the dealloc method:
So, I was wondering why I can't do this instead:
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.
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.