Memory leaks

Discussion in 'iPhone/iPad Programming' started by cjlesh, Jan 18, 2009.

  1. macrumors newbie

    Joined:
    Jan 6, 2009
    #1
    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.
     
  2. macrumors newbie

    Joined:
    Jul 23, 2002
    #2
    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.
     
  3. macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #3
  4. macrumors regular

    Joined:
    Feb 27, 2008
    #4
    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.
     
  5. thread starter macrumors newbie

    Joined:
    Jan 6, 2009
    #5
    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
     
  6. macrumors regular

    Joined:
    Feb 27, 2008
    #6
    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.


    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.
     

Share This Page