4

我觉得我开始在这个问题上失去理智了。

我已经开始使用 SDK 提供的生成的 CoreData 代码来开发 CoreData iOS 应用程序。每当我尝试实例化实体的新实例以便保存它时,就会出现我的问题。

根据 Apple CoreData 教程,这是我在 AppDelegate 中的实例化代码(我将一堆代码移到那里只是为了尝试调试此问题):

NSManagedObjectContext* context = [self managedObjectContext];
if (!context)
{
    NSLog(@"Error"); // I'm not too concerned about my error handling just yet
}

在那之后,这是产生我遇到的错误的行:

Vehicle* vehicle = (Vehicle*)[NSEntityDescription insertNewObjectForEntityForName:@"Vehicle" inManagedObjectContext:context];

有问题的错误是:

Thread 1: EXC_BAD_ACCESS (code=EXC_ARM_DA_ALIGN address=0xdeadbeef)

总而言之,除了内存对齐问题(ARMv7 常见?)之外,我真的不知道这意味着什么,而且我在 Google 上找到的资源对我没有丝毫帮助。

唯一其他相关的代码是 Xcode 在生成项目时提供的“managedObjectContext”方法,因为它首先生成了 managedObjectContext:

- (NSManagedObjectContext *)managedObjectContext
{
    if (__managedObjectContext != nil) {
        return __managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        __managedObjectContext = [[NSManagedObjectContext alloc] init];
        [__managedObjectContext setPersistentStoreCoordinator:coordinator];
    }

    return __managedObjectContext;
}

就像我说的,我在这里超出了我的深度。任何人都可以就我如何解决这个问题提供一些明确的说明吗?

4

2 回答 2

4

很可能__managedObjectContext未初始化(因此具有 的值0xdeadbeef),这在尝试从中读取值时会导致EXC_ARM_DA_ALIGN副作用。

@Kenny Winker EXC_ARM_DA_ALIGN通常来自不是实际类型的访问指针值。例如

char buf[8];
double d = *((double *)buf); // this may cause EXC_ARM_DA_ALIGN

但也可能导致指针中的无效有效,在这种情况下,0xdeadbeef. 例如

double *ptr; // not initialized
double d = *ptr; // this is undefined behaviour, which may cause EXC_ARM_DA_ALIGN or other error

通常很难调试这类错误,这里有一些提示:

  1. 检查所有指针转换(即(double *)(void *)ptr)并尽可能避免它们。
  2. 确保一切都已初始化。
  3. 当它崩溃时,找出导致它崩溃的变量并尝试回溯以找出值的来源。使用调试器查看内存位置有助于找出变量的所有更改。
于 2013-09-17T22:58:06.393 回答
1

我认为显示正在运行的核心数据堆栈可能会有所帮助。我还将展示对象图的一部分..

pragma mark -
#pragma mark Core Data stack

/**
 Returns the managed object context for the application.
 If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
 */


- (NSManagedObjectContext *)managedObjectContext {
    //  NSLog(@"%s", __FUNCTION__);

    if (managedObjectContext_ != nil) {

        return managedObjectContext_;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];

    if (coordinator != nil) {

        managedObjectContext_ = [[NSManagedObjectContext alloc] init];

        [managedObjectContext_ setPersistentStoreCoordinator:coordinator];

    }

    return managedObjectContext_;
}

/**
 Returns the managed object model for the application.
 If the model doesn't already exist, it is created from a starter file.
 */


- (NSManagedObjectModel *)managedObjectModel {
    //NSLog(@"%s", __FUNCTION__);
    if (managedObjectModel_ != nil) {
        return managedObjectModel_;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"DreamCatching" withExtension:@"mom"];
    managedObjectModel_ = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];    
    return managedObjectModel_;
}

/**
 Returns the persistent store coordinator for the application.
 If the coordinator doesn't already exist, it is created and the application's store added to it.
 */
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    //NSLog(@"%s", __FUNCTION__);
    if (persistentStoreCoordinator_ != nil) {
        return persistentStoreCoordinator_;
    }



    NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"DreamCatching.sqlite"];


    // If the expected store doesn't exist, copy the default store.

    //COMMENT / UNCOMMENT THIS TO LOAD / NOT LOAD THE STARTER FILE.
    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath:storePath]) {        
        NSError *error;
        NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Starter" ofType:@"sqlite"];

        if ([[NSFileManager defaultManager] copyItemAtPath:defaultStorePath toPath:storePath error:&error]) 
            NSLog(@"Copied starting data to %@", storePath);
        else 
            NSLog(@"Error copying default DB to %@ (%@)", storePath, error);
    }
    // to below here




    NSURL *storeURL = [NSURL fileURLWithPath:storePath];


    NSError *error = nil;
    persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

    if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType
                                     configuration:nil URL:storeURL options:nil error:&error]) {

        NSLog(@"Error - App Delegate Creating DB %@, %@", error, [error userInfo]);
        abort();
    }    

    return persistentStoreCoordinator_;
}

#pragma mark -
#pragma mark Application's Documents directory

/**
 Returns the path to the application's Documents directory.
 NB SETTINGS ARE NOT IN THIS DIRECTORY, THEY ARE IN THE APPS BUNDLE. CONTROL-CLICK THE APP TO SEE CONTENTS, CONTROL-CLICK THE BUNDLE TO SEE THE PREFS
 */
- (NSString *)applicationDocumentsDirectory {
    //NSLog(@"%s", __FUNCTION__);
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

和模型:

在此处输入图像描述

于 2013-09-24T21:44:24.857 回答