views:

51

answers:

2

Hi,

I have a UITabBarController. One of the tab shows application "bookmarks", these bookmarks are basicly search types saved to a Core Data (SQLLite) database.

When I load my application, go to the bookmark view (bookmarksViewController), it loads a function in my appDelegate (getBookmarks) and shows the result in an table. This works perfect, can switch between views, preform searches in other views switch back. No problem. It load the content every time. BUT... when I load a search view and add a new bookmark, and then switch back to the bookmark view it dies with the error message "EXC_BAD_ACCESS". I have no idea why and how I can solve this.

This is my code:
bookmarksViewController.m

[...]
- (void)viewDidLoad {
    [super viewDidLoad];

 theBookmarks = nil;

    // Set up the edit and add buttons.
    self.navigationItem.rightBarButtonItem = self.editButtonItem;

 [self setTitle:NSLocalizedString(@"Bookmarks", @"bookmarksViewController")];
}

- (void)viewWillAppear:(BOOL)animated { 
 if (theBookmarks != nil)
 {
  NSLog(@"Release it!");
  [theBookmarks release];
 }

 NSLog(@"Appear 1");

 stationenAppDelegate *stationen = (stationenAppDelegate *)[[UIApplication sharedApplication]delegate];

 NSLog(@"Appear 1 - stage 2");

 theBookmarks = [[stationen getBookmarks] retain];

 NSLog(@"Appear 2");

    [super viewWillAppear:animated];
}
[...]

myAppDelegate.m

[...]
/**
 * Bookmarks
 *
 * Type definition
 * 1: Station search
 * 2: Train search
 * 3: Interuption search
 */

- (NSMutableArray*)getBookmarks
{
 NSLog(@"check 1");

 NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

 NSEntityDescription *entity = [NSEntityDescription entityForName:@"bookmarks" 
             inManagedObjectContext:self.managedObjectContext];
 [fetchRequest setEntity:entity];

 NSLog(@"check 2");

 NSError *error;
 NSArray *items = [[self.managedObjectContext
        executeFetchRequest:fetchRequest error:&error] retain];
 NSMutableArray *returnArray = [[[NSMutableArray alloc] initWithArray:items] retain];

 NSLog(@"check 4");

 [fetchRequest release];

 return returnArray;
}

- (void)addBookmark:(bookmarks_object*)theBookmark
{
 BOOL exists = [self checkIfBookmarkExistsUsingBookmark:theBookmark];

 if(!exists)
 {
  bookmarks *saveBookmark = (bookmarks *)[NSEntityDescription insertNewObjectForEntityForName:@"bookmarks"
                    inManagedObjectContext:self.managedObjectContext];

  [saveBookmark setStation_from:theBookmark.station_from];
  [saveBookmark setStation_to:theBookmark.station_to];
  [saveBookmark setAddDate:[[NSDate alloc] init]];
  [saveBookmark setSort:theBookmark.sort];
  [saveBookmark setPlace:[[NSNumber alloc] initWithInt:([[self getBookmarks] count]+1)]];
  [saveBookmark setName:([theBookmark.type isEqualToString:@"1"] || [theBookmark.type isEqualToString:@"2"] ? theBookmark.station_from : [[NSString alloc] initWithFormat:@"%@ -> %@", theBookmark.station_from, theBookmark.station_to])];
  [saveBookmark setType:theBookmark.type];

  [self saveAction];
  [saveBookmark release];
 }
 else {
  NSLog(@"No need to add, %@ already exists!", theBookmark.station_from);
 }
}

- (BOOL)checkIfBookmarkExistsUsingBookmark:(bookmarks_object*)theBookmark
{
 // Get the categories
 NSArray *historyArray = [self getBookmarks];

 BOOL exists = NO;

 for(bookmarks *dbHistory in historyArray)
 {
  if ([theBookmark.type isEqualToString:@"1"] || [theBookmark.type isEqualToString:@"2"])
  {
   if([[dbHistory station_from] isEqualToString:theBookmark.station_from])
   {
    exists = YES;
    continue;
   }
  }
  else if ([theBookmark.type isEqualToString:@"3"])
  {
   if([[dbHistory station_from] isEqualToString:theBookmark.station_from] && 
      [[dbHistory station_to] isEqualToString:theBookmark.station_to])
   {
    exists = YES;
    continue;
   }
  }
  else {
   NSLog(@"Error! Unknown type!");
   return NO;
  }
 }

 return exists;
}
[...]

Stack trace (Flow: Opening app, load bookmarks view, switch to search view, add bookmark, switch back)

2010-09-19 13:51:54.554 stationen[7256:207] Appear 1
2010-09-19 13:51:54.555 stationen[7256:207] Appear 1 - stage 2
2010-09-19 13:51:54.555 stationen[7256:207] check 1
2010-09-19 13:51:54.560 stationen[7256:207] check 2
2010-09-19 13:51:54.562 stationen[7256:207] check 4
2010-09-19 13:51:54.562 stationen[7256:207] Appear 2
2010-09-19 13:52:26.669 stationen[7256:207] check 1
2010-09-19 13:52:26.670 stationen[7256:207] check 2
2010-09-19 13:52:26.671 stationen[7256:207] check 4
2010-09-19 13:52:26.671 stationen[7256:207] No need to add, 230 already exists!
2010-09-19 13:52:30.509 stationen[7256:207] Release it!
2010-09-19 13:52:30.510 stationen[7256:207] Appear 1
2010-09-19 13:52:30.510 stationen[7256:207] Appear 1 - stage 2
Program received signal:  “EXC_BAD_ACCESS”.
A: 

Did you try to use Debugger to see where the error is located?
In Xcode open the Debugger then build the app with Breakpoints. In the simulator do all steps to reproduce the error.
When you'll find the error, the Debugger will stop the app before it crashes and you'll see you implemented methods in the Debugger's view... One or more of them will be highlighted/written in black. In these method there is an error. Clicking in the method you'll see where is the error and then you'll be able to modify that code to resolve the error...

Hope this can help you! :)

Matthew
Yes, I have... and thats the point that doesn't make sence either. I put breakpoints at the "check 1" and ad the calling of the "getBookmarks" function in the bookmark view. It dies in between it seems to me.
Paul Peelen
Unfortunaley I'm not an expert because I never developed before starts with iPhone development. Reading on the web, EXC_BAD_ACCESS is a common problem can be resolved using NSZombieEnabled, like Mark said. You can take a look to these posts: http://www.codza.com/how-to-debug-exc_bad_access-on-iphone and http://www.touch-code-magazine.com/how-to-debug-exc_bad_access/ Hope this can help you! ;)
Matthew
A: 

The problem was that the appDelegate somehow was released between the time allocating it and using it. I add a retain to it and releasing it just after.

Found it using NSZombieEnabled as suggested by Mark. Thanks!

Paul Peelen
Oh, I didn't refresh the page and I commented the post :D Sorry!
Matthew
Np. It just got a bit weirder. I added an other search view and the same thing happens here. But now I am retaining the object. The code ´´stationenAppDelegate *stationen = (stationenAppDelegate *)[[[UIApplication sharedApplication]delegate] retain];´´ is deallocated directly.
Paul Peelen
You shouldn't be retaining or releasing the app delegate anywhere. If you have a property that points to the app delegate make it an `assign` instead of `release` in the property declaration.
TechZen
I found that out yesterday... even worst... I released the station object at several location meaning that I release my appDelegate... baaad coding, :(
Paul Peelen