iOS:生成的CoreData代码中的EXC_ARM_DA_ALIGN

时间:2012-06-09 04:06:45

标签: iphone ios xcode core-data automatic-ref-counting

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

我已经开始使用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;
}
像我说的那样,我离开了我的深度。关于如何解决这个问题,任何人都可以提供一点清晰度吗?

2 个答案:

答案 0 :(得分:4)

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

@Kenny Winker EXC_ARM_DA_ALIGN通常来自非实际类型的访问指针值。 e.g。

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

但也可能导致指针无效,在本例中为0xdeadbeef。 e.g。

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. 当它崩溃时,找出导致它崩溃的变量,并尝试追溯以找出值来自何处。使用调试器来查看内存位置有助于找出变量的所有更改。

答案 1 :(得分: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];
}

和模型:

enter image description here