Help ! Question about "Release Object".

Discussion in 'iOS Programming' started by cthesky, Oct 9, 2011.

  1. cthesky, Oct 9, 2011
    Last edited by a moderator: Oct 9, 2011

    cthesky macrumors member

    Joined:
    Aug 21, 2011
    #1
    Hi,

    I am newbie of develop Iphone application. I am reading a code sample. I have a question for a part of code.

    Here is my code:

    Code:
    [B][U]TableViewController.h[/U][/B]
    
    @interface TableViewController : UITableViewController {
         NSArray * tableDataList; 
    }
    
    @property (nonatomic, retain) NSArray * tableDataList;
    
    @end
    
    [B][U]TableViewController.m[/U][/B]
    
    @implementation TableViewController
    
    @synthesize tableDataList;
    
    -(void) viewDidLoad {
        NSArray * [COLOR="RoyalBlue"]tempArray[/COLOR] = [[NSArray alloc] initWithObjects:@"Item 1", @"Item 2", @"Item 3", nil];
        self.tableDataList = tempArray; 
    } 
    
    @end
    
    So, my question is in the viewDidLoad() method above, it does not release the "tempArray" object. But why it does not cause a memory leak problem?

    Hope someone can give me some comments and explanation. Thanks a lot. :)
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    The array will be leaked. The memory management rules clearly say the alloc needs to be matched with a release.

    If the tableDataList property has the assign attribute instead of the retain attribute, it would not leak. But IMHO the attributes on the property are correct. The lack of a release is the error.
     
  3. cthesky thread starter macrumors member

    Joined:
    Aug 21, 2011
    #3
    Hi Jiminaus,

    yes, the alloc needs to be matched with a release. But what happen now is when I trace the memory leak problem by using Instrument, there is no any leaked block found. I am quite confused about this. Do you know why? Can anyone tell me the reason?

    Any comments are welcome. Thanks a lot. :)
     
  4. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #4
    Can you tell us in detail how you're actually testing this?
     
  5. cthesky thread starter macrumors member

    Joined:
    Aug 21, 2011
    #5
    ok. I test this by using the "Instrument - Leaks" in Xcode4. In Xcode4, I go to "Product" -> "Profile" -> choose "Leaks". Then the program will be run and any leaks found will show in the "Instrument Dialog Box".

    So, am I doing something wrong? Do you test the memory leak problem before? Is it using "Instrument" in Xcode ?

    Thanks for your reply. :)
     
  6. ChOas macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #6
    Well, as long as you have a pointer to the object it is not leaked by definition.

    As soon as you point tableDataList to something else (without releasing it) the original will leak.
     
  7. cthesky thread starter macrumors member

    Joined:
    Aug 21, 2011
    #7
    Hi, for second sentence in your replied "As soon as you point tableDataList to something else (without releasing it) the original will leak.", I still not very understand... Can you explain more about this? Thanks for your reply. :)
     

Share This Page