array retain in table view objective c

I have a small confusion. When I print my array in tableView dataSource method numberOfRowsInSection , my App crashes.

This is my code : in .h file

@interface AddColor : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
UITableView *tblView;
NSArray *arrayColors;
}
@property(nonatomic,retain)NSArray *arrayColors;

@end

In .m file

@synthesize arrayColors;
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:NO];

     arrayColors = [NSArray arrayWithObjects:@"blackColor", @"darkGrayColor", @"lightGrayColor", @"whiteColor",  @"grayColor", @"redColor", @"greenColor", @"blueColor", @"cyanColor", @"yellowColor", @"magentaColor", @"orangeColor", @"purpleColor", @"brownColor", nil];


    tblView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain];
    tblView.delegate=self;
    tblView.dataSource=self;
    [self.view addSubview:tblView];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

   NSLog(@"%d",[arrayColors count]);//App crashes here.
   return [arrayColors count];
}

My app crashes when I print [arrayColors count];

I find the solution for crash, I simply retain the array in viewDidLoad

[arrayColor retain];

and now working fine. but why my app crashes before when I print the [arrayColor count];

Answers


+ [NSArray arrayWithObjects:]

creates an autoreleased array - it's unsepcified when it is released, but most likely it's released when the viewDidLoad method returns, so it's an invalid (garbage) pointer that you access in [arrayColor count].

By retaining the array, you get rid of the deallocation error, but now you're leaking memory. The general approach of solving this the right way is to allocate and initialize the array in one of the initializer methods, like

- (id)init
{
    if ((self = [super init])) {
        arrayColors = [[NSArray alloc] initWithObjects:..., nil];
    }
    return self;

}

and then purge it in - dealloc in order not to leak memory:

- (void)dealloc
{
    [arrayColors release];
    [super dealloc];

}

More on the topic on Apple Developer.


  1. Try using [self.arrayColors count] - use the ivar getters and setters to access the retained property

  2. As an aside, I would strongly advise against hardcoding the size of your tableview. At the very least set the autoresizing mask.


Need Your Help

running error when calling a c library function from assmbly code

c++ c debugging assembly linker

I was trying to develop a better understanding of the linkers and how they work, so I tried to call the simple function(printf) from the c library (MSVCRTD.lib) but with assembly code on MASM.

how to sav blob as a imagefile in couch db?

javascript jquery couchdb

I am using couchdb with jquery couch api.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.