Iphone 奇怪的EXC\u坏访问,我做错了什么?

Iphone 奇怪的EXC\u坏访问,我做错了什么?,iphone,core-data,ios4,exc-bad-access,Iphone,Core Data,Ios4,Exc Bad Access,我有一个超宽带控制器。其中一个选项卡显示应用程序“书签”,这些书签基本上是保存到核心数据(SQLLite)数据库的搜索类型 加载应用程序时,转到书签视图(bookmarksViewController),它在我的appDelegate(getBookmarks)中加载一个函数,并在表格中显示结果。这工作非常完美,可以在视图之间切换,在其他视图中执行搜索切换回来。没问题。它每次都加载内容。 但是当我加载一个搜索视图并添加一个新书签,然后切换回书签视图时,它将消失,并显示错误消息“EXC\u BAD

我有一个超宽带控制器。其中一个选项卡显示应用程序“书签”,这些书签基本上是保存到核心数据(SQLLite)数据库的搜索类型

加载应用程序时,转到书签视图(
bookmarksViewController
),它在我的appDelegate(
getBookmarks
)中加载一个函数,并在表格中显示结果。这工作非常完美,可以在视图之间切换,在其他视图中执行搜索切换回来。没问题。它每次都加载内容。 但是当我加载一个搜索视图并添加一个新书签,然后切换回书签视图时,它将消失,并显示错误消息“
EXC\u BAD\u ACCESS
”。 我不知道为什么以及如何解决这个问题

这是我的代码:
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];
}
[...]
[...]
/**
 * 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;
}
[...]
myAppDelegate.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];
}
[...]
[...]
/**
 * 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;
}
[...]
堆栈跟踪(流程:打开应用程序、加载书签视图、切换到搜索视图、添加书签、切换回)


您是否尝试使用调试器查看错误所在的位置?
在Xcode中打开调试器,然后使用断点构建应用程序。在模拟器中,执行所有步骤以再现错误。
当您发现错误时,调试器将在应用程序崩溃之前停止应用程序,您将在调试器的视图中看到您实现的方法。。。其中一个或多个将以黑色突出显示/书写。在这些方法中有一个错误。单击方法,您将看到错误在哪里,然后您将能够修改该代码以解决错误


希望这能对你有所帮助!:)

问题是appDelegate在分配和使用它的时间之间以某种方式被释放。 我给它添加了一个retain,然后就发布了它


按照Mark的建议,使用
NSZombieEnabled
找到它。谢谢

请设置断点并检查[stationen getBookmarks]的返回结果是否为零或stationen是否为零。。还有什么原因让你保留这么多?!您是否已将NSzombie设置为启用状态?在跟踪这些错误时,它非常有帮助:不,但我会看一看。是的,我有。。。这一点也没有意义。我将断点放在“check1”处,并在bookmark视图中调用“getBookmarks”函数。不幸的是,我不是一个专家,因为我从来没有开发过iPhone。在网上阅读,EXC_BAD_访问是一个常见的问题,可以使用NSZombieEnabled解决,就像马克说的那样。你可以看看这些帖子:希望这能对你有所帮助!;)哦,我没有刷新页面,我评论了帖子:对不起!NP只是有点奇怪。我添加了另一个搜索视图,这里也发生了同样的事情。但现在我保留了这个对象。代码´´´StationAppDelegate*stationen=(StationAppDelegate*)[[[UIApplication sharedApplication]委托]保留];已直接解除分配。您不应在任何地方保留或释放应用程序委派。如果您有一个指向应用程序委托的属性,请在属性声明中将其设置为
assign
,而不是
release
。我昨天发现。。。甚至最糟糕的。。。我在几个位置释放了station对象,这意味着我释放了appDelegate。。。baaad编码:(