53540

EXC_BAD_ACCESS crash when switching back and forth between views

Question:

I'm getting a EXC_BAD_ACCESS crash when switching back and forth between views. I'm having a problem finding the cause of this crash. In the simulator it always goes back to the main.m file and reports the crash in it.

But on my device the EXC_BAD_ACCESS show up on my custom UITableViewCell when I release it in the dealloc method. If I enable NSZombieEnabled my app doesn't crash at all.

Here is the .h file

#import <UIKit/UIKit.h> #define kWinsAmountTagValue 2 // how many wins you have #define kWinningsAmountTagValue 3 // how much money you won @interface MyStatsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UINavigationBarDelegate, UINavigationControllerDelegate>{ NSArray *list; UITableView *theTable; UITableViewCell *theCell; } @property (nonatomic, retain) NSArray *list; @property (nonatomic, retain) IBOutlet UITableView *theTable; @property (nonatomic, retain) IBOutlet UITableViewCell *theCell; // dealloc and cleanup -(void) dealloc; // misc methods -(void)loadData; // demo data -(NSArray *)tableData; @end

Here is my .m file

#import "MyStatsViewController.h" @implementation MyStatsViewController @synthesize list; @synthesize theTable; @synthesize theCell; #pragma mark - dealloc and cleanup - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; NSLog(@"Memory Warning"); // Release any cached data, images, etc that aren't in use. } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; self.list = nil; self.theTable = nil; self.theCell = nil; } - (void)dealloc { [super dealloc]; [list release]; [theTable release]; [theCell release]; } #pragma mark - misc methods -(void) loadData { self.list = [self tableData]; } #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. } -(void)viewWillAppear:(BOOL)animated { [self loadData]; [theTable reloadData]; } #pragma mark - Table Data Source Methods -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [list count]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier =@"MyStatsCustomCellIdentifer"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; NSUInteger row = [indexPath row]; if (cell == nil) { if (row == [list count] -1) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } else { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyStatsCustomCell" owner:self options:nil]; if ([nib count] > 0) { cell = self.theCell; } else { NSLog(@"failed to load MyStatsCustomCell"); } } } // Add custom stuff here for rows //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; if (row == [list count] -1) { cell.textLabel.text = [list objectAtIndex:row]; } else { UILabel *prizeLevel = (UILabel *)[cell viewWithTag:kPrizeLevelTagValue]; prizeLevel.text = [[list objectAtIndex:row] objectForKey:@"prizeLevel"]; UILabel *winsAmount = (UILabel *)[cell viewWithTag:kWinsAmountTagValue]; winsAmount.text = [[list objectAtIndex:row] objectForKey:@"winsAmount"]; UILabel *winningsAmount = (UILabel *)[cell viewWithTag:kWinningsAmountTagValue]; winningsAmount.text = [[list objectAtIndex:row] objectForKey:@"winningsAmount"]; } //NSLog(@"theCell Retain: %i",[theCell retainCount]); return cell; } #pragma mark - Table View Delegate Methods -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; } #pragma mark - demo data -(NSArray *)tableData { NSArray *prizeLevels = [[NSArray alloc] initWithObjects: @"6-of-6", @"5-of-6", @"4-of-6",@"3-of-6", nil]; NSArray *winsAmount = [[NSArray alloc] initWithObjects: @"0", @"0", @"2", @"100", nil]; NSArray *winngingsAmount = [[NSArray alloc] initWithObjects: @"$0",@"$0", @"$45.50",@"$125.00", nil]; NSMutableArray *myGames = [[[NSMutableArray alloc] init] autorelease]; for (int i = 0; i < [prizeLevels count]; i++) { NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; [dict setObject:[prizeLevels objectAtIndex:i] forKey:@"prizeLevel"]; [dict setObject:[winsAmount objectAtIndex:i] forKey:@"winsAmount"]; [dict setObject:[winngingsAmount objectAtIndex:i] forKey:@"winningsAmount"]; [myGames addObject:dict]; [dict release]; } [prizeLevels release]; [winsAmount release]; [winngingsAmount release]; [myGames addObject:@"Spent: $1250.00"]; return myGames; } @end

Any help would be appreciated.

Answer1:

It is a good practice to clean up class's own variables before calling the super's destructor. A lot more details can be found here: <a href="https://stackoverflow.com/questions/909856/why-do-i-have-to-call-super-dealloc-last-and-not-first" rel="nofollow">Why do I have to call super -dealloc last, and not first?</a>.

Recommend

  • PyQt QAbstractTableModel checkbox not checkable
  • Filter NSMutableDictionary data in UITableView
  • page-object gem seems not working
  • page-object gem seems not working
  • How should I store twitter token so users don't have to go to twitter_oauth everytime?
  • DuplicateKeyException in LINQ, but I've set auto increment and auto sync
  • MATLAB: MEX matrix division gives different result than m-file
  • UIScrollView layoutSubviews behavior changes in iOS 5?
  • Get all data in html table into array
  • _iVar vs. iVar_ for variable naming [closed]
  • How can I add a gradient that spans two views?
  • how can i do UIView animateWithDuration in viewDidLoad? ios7
  • access an alertView's calling view
  • playing video in iOS 5
  • MSVC 2015 /Wall has lots of not useful messages
  • XMPP push notifications causing problems (delay + duplications) in messages
  • Interpret strings as packed binary data in C++
  • Private chat system MYSQL query to display last message of sender/receiver
  • How can I use the auto-generated Id of a record I added in a subsequent task?
  • C++: Use input string as variable name
  • how to insert a new column in a dataset with values if it satisfies a statement
  • How can I figure out what UITableViewCell I'm clicking on during a search?
  • What is the effect of 'explicit' keyword on the Return Value Optimization (RVO)?
  • How to use the File System Events API in Swift?
  • Column Nullability/Optionality: NULL vs NOT NULL
  • What's the benefit of the trailing apostrophe in character literals
  • iOS - MKOverlayView custom view rect fills works, but line draws do not
  • EF 4.1 DBContext AutoDetectChangesEnabled
  • Backward compatibility of Python 3.5 for external modules
  • abstracting over a collection
  • Can't remove headers after they are sent
  • Possible to get mouse events fired when cursor is outside page?
  • Filter strings with regex before casting to numeric
  • C++ pointer value changes with static_cast
  • WPF ICommand CanExecute(): RaiseCanExecuteChanged() or automatic handling via DispatchTimer?
  • RectangularRangeIndicator format like triangular using dojo
  • Cannot Parse HTML Data Using Android / JSOUP
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Getting Messege Twice Using IMvxMessenger
  • How to load view controller without button in storyboard?