views:

1442

answers:

2

Hopefully someone can help me debug this problem as EXC_BAD_ACCESS is the only error I receive. I've also tried turning on NSZombieEnabled, but don't get any more information as far I can tell.

The problem. I have four entities:

A -> B -> C -> D

Where the arrow symbolizes a set: "A" contains a to-many relation to "B", "B" a to-many relation to "C" etc. For creating the entity I use:

id dto = [NSEntityDescription insertNewObjectForEntityForName:@"A" 
    inManagedObjectContext:context];
NSLog(@"DTO: %@", dto);

and this seems to work for A, B and C. However, when using it on entity D the app crashes with EXC_BAD_ACCESS. The problem seems to occur when accessing the object, as the program runs successfully when commenting out NSLog and other methods accessing the dto-object.

Update:

Console output

GNU gdb 6.3.50-20050815 (Apple version gdb-1346) (Fri Sep 18 20:40:51 UTC 2009)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".sharedlibrary apply-load-rules all
Attaching to process 3100.
Program received signal:  “EXC_BAD_ACCESS”.
warning: Unable to restore previously selected frame.
No memory available to program now: unsafe to call malloc
warning: check_safe_call: could not restore current frame

Data Formatters temporarily unavailable, will re-try after a 'continue'. (Not safe to call dlopen at this time.)
warning: Unable to restore previously selected frame.

Stack
The stack trace is very large (?), when debugging it loads "62826 stack frames". Showing a part of it: alt text alt text
Lines #8-#41 is repeated to around frame #62500.

A: 

It looks like you need to retain your new object. The description of that method states that the object returned is autoreleased, which means your object is probably getting released before you have a chance to use it. Change your code to look like this:

id dto = [[NSEntityDescription insertNewObjectForEntityForName:@"A" 
    inManagedObjectContext:context] retain];
NSLog(@"DTO: %@", dto);

Be sure to release your object when you're done with it to avoid memory leaks. If you'll be done with the object (or if it will be retained by another object) by the time dto is out of scope, you can autorelease it to let the autorelease pool take care of it for you:

id dto = [[[NSEntityDescription insertNewObjectForEntityForName:@"A" 
    inManagedObjectContext:context] retain] autorelease];
Aaron
Thank's for your answer. Unfortunately it makes no difference if I retain or not - same crash.
Mads Mobæk
+5  A: 

So whenever there are that many stack frames, it means there is some sort of infinite recursion going on. My guess is that when creating a D object, there is some code that automatically creates something else, which in turn is creating another D and there is a loop that is unterminated. I would start by checking any Key Value Observers or NSManagedObject Overrides

coneybeare
Ah! Found the solution! In my D object i had a property named "description". That corresponds to the many calls to description in the stack frames. I renamed it to "desc" and it now works. Would be nice if I found a list with all the reserved words...
Mads Mobæk
It's not, strictly speaking, a reserved word. But it is a method defined by NSObject: http://developer.apple.com/iphone/library/documentation/cocoa/reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/occ/intfm/NSObject/description
Sixten Otto
Pay attention to warnings! There is a warning when you try and name a Core Data property "description". Ignoring warnings in Objective-C is always a bad idea.
Marcus S. Zarra
Strange, I did the same thing and was lucky enough to find this thread, but I was not getting any compiler warnings about naming the entity property as description..Thanks guys.
Paul Shapiro