Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-31-2010, 10:07 PM   #1 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 13
Question ERR_BAD_ACCESS and NSArray weirdness

I tried a few things and I'm stumped by this. In a UIView object I have an NSArray object declared in my @interface, and set in - (void)awakeFromNib. The UIView is a data source for its UITableView, and to get the number of rows I'm calling the count method, which causes an ERR_BAD_ACCESS. Here is the weird part, if I do
Code:
NSLog(@"%@", [self.streamsArray count]);
I get an ERR_BAD_ACCESS, but if I call
Code:
NSLog(@"%@", self.streamsArray);
everything prints out fine. So why would it be that calling the count method doesn't work?
axiixc is offline   Reply With Quote
Old 01-31-2010, 11:10 PM   #2 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 580
Default

Quote:
Originally Posted by axiixc View Post
So why would it be that calling the count method doesn't work?
The string specifier %@ is only used for printing Objective-C objects. The count method returns an NSUInteger, which is not an object. Change the format specifier to %u, which is used for printing unsigned integers, and you should be fine.
ChrisL is offline   Reply With Quote
Old 02-01-2010, 10:34 PM   #3 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 13
Default

Sorry I guess I over abbreviated my code. What I'm actually running is this
Code:
@interface RootStreamPicker : UIViewController <UITableViewDataSource, UITableViewDelegate> {
   NSArray *streams;
}
@end

@implementation RootStreamPicker

- (void)awakeFromNib {
   streams = [NSArray arrayWithObjects:@"One", @"Two", @"Three", @"Four", @"Five", nil];
}

#pragma mark UITableViewDataSource Protocol Implimentation
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   return [streams count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   static NSString *cellID = @"StreamPickerCell";
   
   UITableViewCell *aCell = [tableView dequeueReusableCellWithIdentifier:cellID];
   if (aCell == nil)
   {
      aCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
   }
   
   aCell.textLabel.text = [streams objectAtIndex:indexPath.row];
   
   return aCell;
}

#pragma mark UITableViewDelegate Protocol Implimentation
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
and when it gets to the tableView:numberOfRowsInSection: method I get an ERR_BAD_ACCESS called. I added streams as a property and then called it only though self.streams, seemed to fix it. Dunno why, anyone know? I'd like to avoid such problems in the future.
axiixc is offline   Reply With Quote
Old 02-01-2010, 10:53 PM   #4 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 580
Default

The error is in this line
Code:
   streams = [NSArray arrayWithObjects:@"One", @"Two", @"Three", @"Four", @"Five", nil];
The arrayWithObjects methods returns an autoreleased array. You can tell this because the method name doesn't begin with "new", "alloc", or "copy". This means you have to explicitly retain the result, or it will be deallocated whenever the current autorelease pool is drained. This is why you're getting the bad access error -- the memory has already been automatically reclaimed by the time you're trying to use it.

You could fix this by simply retaining streams, like so:
Code:
streams = [NSArray arrayWithObjects:@"One", @"Two", @"Three", @"Four", @"Five", nil];
[streams retain];
But, your solution of using a retained property is less error prone than accessing the instance variable directly, since the generated property methods will handle the memory management for you. In either case, make sure you release streams in your dealloc method.

If any of this is confusing, a good place to start would be the Cocoa Memory Management Programming Guide. BrianSlick also wrote a really nice beginner's guide to using properties, which might be helpful as well.
ChrisL is offline   Reply With Quote
Old 02-01-2010, 10:56 PM   #5 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 13
Default

Added it, tried it, works. Thank you very much for that explanation. As a former PHP guy there is a lot of stuff I never had to think about before on the iPhone.
axiixc is offline   Reply With Quote
Reply

Bookmarks

Tags
err_bad_access, nsarray, uitableviewdatasource

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,765
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:23 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0