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

cthesky

macrumors member
Original poster
Aug 21, 2011
91
0
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. :)
 
Last edited by a moderator:
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.
 
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.

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. :)
 
Can you tell us in detail how you're actually testing this?

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. :)
 
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. :)

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.
 
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.

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. :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.