What cause EXC_BAD_ACCESS?

  • Hi guys,

    I'm sorry I'm a newbie of Cocoa so that I don't know if I can show
    you appropriate situation, but I'll try.

    An EXC_BAD_ACCESS error comes only after my app reads my custom
    format type of data and reads document nib file.

    1. When new document is selected, no problem.
    2. When open document is selected and the app reads one of three
    formats which Core Data supports, no problem.
    3. Only after reading custom formatted file and after
    windowControllerDidLoadNib gets called, EXC_BAD_ACCESS comes.
        The windowControllerDidLoadNib method calls [super
    windowControllerDidLoadNib] and detects how each attribute of
    managedObjects
        has been set.

    It looks fine that the custom formatted data is read, and the objects
    created and initialized with the data seem to be fine.
    Because the value of each attribute of managedObjects was what I
    expected.

    Entity: Employ
      name(attribute)
      department(one relationship)

    Entity: Department
      dept_name(attribute)
      employees(many relationship)

    @interface DepartmentMO : NSManagedObject
    {
    }

    - (int) convertCTypeOfDataToManagedObject:(NSData*)data
                       position:(unsigned long*)position
                     withVersion:(int)version;

    This method reads a custom type of data and set all attributes of
    entity "Department" using setValue:forKey method.
    In this method, it creates EmployeeMO by using
    insertNewObjectForEntityForName: inManagedObjectContext: and
    obtains employees relationship using mutableSetValueForKey: and add
    the created object to the attribute using addObject.
    Of cause department attribute of the EmployeeMO object set this
    department object (self) using setValue:forKey:.

    @ interface EmployMO : NSManagedObject
    {
    }

    - (int) convertCTypeOfDataToManagedObject:(NSData*)data
                       position:(unsigned long*)position
                     withVersion:(int)version;

    This method reads a custom type of data and set all attributes of
    this entity using setValue:forKey method.

    The following steps are in one function.

    DepartmentMO* department = nil;
    for (i = 0 ; i < counter; ++i )
    {
      department = (DepartmentMO*)[NSEntityDescription
    insertNewObjectForEntityForName:@"Department"
        inManagedObjectContext:context];

      (void)[department convertCTypeOfDataToManagedObject:data
    position:&pos withVersion:version];

      [context processPendingChanges];
    }

    In the convertCTypeOfDataToManagedObject:position:withVersion: of
    DepartmentMO

    NSMutableSet * inObjects = [self mutableSetValueForKey:@"employees"];
    for (i = 0; i < numOfHistory; ++i)
    {
      employee = (EmployMO*)[NSEntityDescription
    insertNewObjectForEntityForName:@"Employ"
        inManagedObjectContext:[self managedObjectContext]];


      (void)[employee convertCDataToManagedObject:data position:position
    withVersion:version ];

      [employee setValue:self forKey:@"department"];

      [inObjects addObject:employee];
    }

    Thank you for reading this poor explanation. I hope what mentioned
    above is understandable.

    Norio Ota
  • On 2007 Sep, 30, at 7:26, Norio Ota wrote:

    > I'm sorry I'm a newbie of Cocoa so that I don't know if I can show
    > you appropriate situation, but I'll try.
    >
    > An EXC_BAD_ACCESS error comes only after my app reads my custom
    > format type of data and reads document nib file.

    Maybe someone else can, but I don't see anything wrong with the code
    you've posted.  The problem may be in some code which you did not post.

    EXC_BAD_ACCESS is usually caused by sending a message to an invalid
    pointer, or to an object which has been released.  The quick and
    dirty way to find a reproducible problem like this is to add many
    NSLog(@"1234") statements to the suspect sections of code, run, and
    then study the code after the last NSLog that prints.  (My life has
    been much more fun since I made a macro to do this.)

    You should also look at NSEnumerator documentation and get into the
    habit of using this instead of for(;;;), (unless you are modifying
    the collection being enumerated).  NSEnumerator is more convenient
    and conventional to use in Cocoa.
  • On 30 Sep 2007, at 9:26 AM, Norio Ota wrote:
    > An EXC_BAD_ACCESS error comes only after my app reads my custom
    > format type of data and reads document nib file.
    >
    > 1. When new document is selected, no problem.
    > 2. When open document is selected and the app reads one of three
    > formats which Core Data supports, no problem.
    > 3. Only after reading custom formatted file and after
    > windowControllerDidLoadNib gets called, EXC_BAD_ACCESS comes.
    > The windowControllerDidLoadNib method calls [super
    > windowControllerDidLoadNib] and detects how each attribute of
    > managedObjects
    > has been set.
    >
    > It looks fine that the custom formatted data is read, and the
    > objects created and initialized with the data seem to be fine.
    > Because the value of each attribute of managedObjects was what I
    > expected.

    You don't say what you learned from the debugger. Does any of your
    code appear in the stack trace?

    — F
  • Am 30.09.2007 um 16:26 schrieb Norio Ota:
    > An EXC_BAD_ACCESS error comes only after my app reads my custom
    > format type of data and reads document nib file.

      Bad Access means that you're trying to access an invalid pointer,
    which is usually due to either a pointer variable that wasn't
    initialized correctly, or accessing a pointer that has been disposed
    of already. Often this happens because someone who doesn't own a
    particular object called release or autorelease on it, or because you
    forgot to retain an object that you're keeping around in an instance
    variable.

      I wrote an article a while ago about memory management and how it
    works. That may help a bit, because I spent a lot of time drawing
    pictures that show memory in a nice visual fashion:

    <http://www.zathras.de/howmemorymanagementworks.htm>

    It starts with the bare basics you probably already know, but if you
    just plow on, it should explain things more thoroughly. The article
    isn't really specific to Cocoa, so if you want the whole story with
    explanations specific to Cocoa, you'll want to listen through Steve
    "Scotty" Scott's memory management podcast:

    <http://latenightcocoa.com/node/57>

    Before that, you may want to re-read Cocoa's memory management rules
    on Apple's web site here:

    <http://developer.apple.com/documentation/Cocoa/Conceptual/
    MemoryMgmt/Tasks/MemoryManagementRules.html
    >

    They're short and sweet, and by first reading 'em and then hearing
    'em, you're guaranteed to get everything hammered home :-)

    Cheers,
    -- M. Uli Kusterer
    http://www.zathras.de
  • Jerry, thanks and go see NSEnumerator documentation,

    Fritz, thanks for your reply.

    Here is the stack where SIGTRAP comes.

    #0    0x90a619c1 in _objc_error
    #1    0x90a619f8 in __objc_error
    #2    0x90a60060 in _freedHandler
    #3    0x927d88cf in NSPopAutoreleasePool
    #4    0x93281e28 in -[NSApplication run]
    #5    0x93275d2f in NSApplicationMain
    #6    0x00002c3b in main at main.m:13

    Then at next step, EXC_BAD_ACCESS comes. Cocoa is a black box to me
    so that I can't figure out what object or value is invalid.

    #0    0x90a619f8 in __objc_error
    #1    0x90a60060 in _freedHandler
    #2    0x927d88cf in NSPopAutoreleasePool
    #3    0x93281e28 in -[NSApplication run]
    #4    0x93275d2f in NSApplicationMain
    #5    0x00002be0 in main at main.m:13

    And I place NSLog in windowControllerDidLoadNib like below.

    - (void)windowControllerDidLoadNib:(NSWindowController *)
    windowController
    {
        [super windowControllerDidLoadNib:windowController];

        NSManagedObjectContext * context = [self managedObjectContext];
        NSSet* set = [context registeredObjects];
        NSLog ([set description]);
    }

    The result of the NSLog above is the following:

    2007-10-01 10:36:48.036 DepartmentEmployee[390] <NSCFSet: 0xd4790b0>
    (< DepartmentMO: 0xd477b20> (entity: Department; id: 0xd477bc0 <x-
    coredata:///Department/t8EF2ED34-4D99-441A-9F9E-107AEB9DB962> ; data: {
        employees = (
            0xd4784a0 <x-coredata:///Employ/t5811C18C-F57F-4FB2-A0C4-
    AC34E119A99B>,
            0xd4781c0 <x-coredata:///Employ/
    t98E09B59-0894-480D-9BA0-55C8F58BB9E0>,
            0xd478770 <x-coredata:///Employ/tA69BDE99-E0D4-4507-A8D7-
    F1A1665F6D5E>,
            0xd478b20 <x-coredata:///Employ/tBD106862-9275-4AB5-
    B603-6DAE06F4F93C>,
            0xd477ef0 <x-coredata:///Employ/t3F17FB95-04A2-4344-
    A09A-7CEE58E31F1C>
        );
        dept_name = TEST;
    }), <DepartmentMO: 0xd46c3a0> (entity: Department; id: 0xd46c450 <x-
    coredata:///Department/t47403729-85C5-42D0-8420-114648FEF148> ; data: {
        employees = (0xd474960 <x-coredata:///Employ/tEE0CA37A-1D8C-4F76-
    A1F8-51AC38DE8A6E>);
        dept_name = "51-2?_U3000?_U30db?_U30f3?_U30c0?_U30a8?_U30ec
    ?_U30e1?_U30f3?_U30c8";
    }), <EmployMO: 0xd474930> (entity: Employ; id: 0xd474960 <x-
    coredata:///Employ/tEE0CA37A-1D8C-4F76-A1F8-51AC38DE8A6E> ; data: {
        department = 0xd46c450 <x-coredata:///Department/
    t47403729-85C5-42D0-8420-114648FEF148>;
        name = nil;
    }), <EmployMO: 0xd478b70> (entity: Employ; id: 0xd478b20 <x-
    coredata:///Employ/tBD106862-9275-4AB5-B603-6DAE06F4F93C> ; data: {
        department = 0xd477bc0 <x-coredata:///Department/
    t8EF2ED34-4D99-441A-9F9E-107AEB9DB962>;
        name = nil;
    }), <EmployMO: 0xd476ed0> (entity: Employ; id: 0xd476eb0 <x-
    coredata:///Employ/t2DE0457E-4138-4D02-AE71-BC40F31CD2D0> ; data: {
        department = 0xd476b80 <x-coredata:///Department/
    tFCBB6254-2E5D-477E-B9B8-4B12D33C37B4>;
        name = nil;
    }), <DepartmentMO: 0xd475cb0> (entity: Department; id: 0xd475ce0 <x-
    coredata:///Department/tFBBE3019-1B85-4DEE-817C-4BDFCDACFA7C> ; data: {
        employees = (0xd476140 <x-coredata:///Employ/tFC4396BF-F05F-4376-
    AD19-040726DFFCE6>);
        deductKind = 2;
        dept_name = "61-2?_U3000?_U97f3?_U97ff?_U6a5f?_U5668?_Uff08
    ?_U4e2d?_U53e4?_Uff09";
    }), <EmployMO: 0xd477440> (entity: Employ; id: 0xd477400 <x-
    coredata:///Employ/tE7FC0BFB-DE93-4B60-B501-F8435C2E95C8> ; data: {
        department = 0xd476b80 <x-coredata:///Department/
    tFCBB6254-2E5D-477E-B9B8-4B12D33C37B4>;
        name = nil;
    }), <EmployMO: 0xd478330> (entity: Employ; id: 0xd4781c0 <x-
    coredata:///Employ/t98E09B59-0894-480D-9BA0-55C8F58BB9E0> ; data: {
        department = 0xd477bc0 <x-coredata:///Department/
    t8EF2ED34-4D99-441A-9F9E-107AEB9DB962>;
        name = nil;
    }), <DepartmentMO: 0x332590> (entity: Department; id: 0x3f74c0 <x-
    coredata:///Department/t65DD1C8B-658B-4E5C-A430-CD2C39F5AD94> ; data: {
        employees = (0xd475640 <x-coredata:///Employ/
    t7CDB6496-0E29-4ACB-97B0-BE76A7C8510B>);
        dept_name = "61-1?_U3000?_U30d1?_U30bd?_U30b3?_U30f3";
    }), <EmployMO: 0xd476900> (entity: Employ; id: 0xd4768e0 <x-
    coredata:///Employ/t9FA3775A-645C-4619-91E8-C885E04D7E32> ; data: {
        department = 0xd4765a0 <x-coredata:///Department/
    tF81EC534-9B7C-4566-BAA1-A37ABB657FDA>;
        name = nil;
    }), <EmployMO: 0xd477810> (entity: Employ; id: 0xd4777c0 <x-
    coredata:///Employ/tA08507B0-09F9-4B34-8E6C-EE72B38BA4F1> ; data: {
        department = 0xd476b80 <x-coredata:///Department/
    tFCBB6254-2E5D-477E-B9B8-4B12D33C37B4>;
        name = nil;
    }), <EmployMO: 0xd476160> (entity: Employ; id: 0xd476140 <x-
    coredata:///Employ/tFC4396BF-F05F-4376-AD19-040726DFFCE6> ; data: {
        department = 0xd475ce0 <x-coredata:///Department/
    tFBBE3019-1B85-4DEE-817C-4BDFCDACFA7C>;
        name = nil;
    }), <EmployMO: 0xd478740> (entity: Employ; id: 0xd478770 <x-
    coredata:///DeductHistory/tA69BDE99-E0D4-4507-A8D7-F1A1665F6D5E> ;
    data: {
        department = 0xd477bc0 <x-coredata:///Department/
    t8EF2ED34-4D99-441A-9F9E-107AEB9DB962>;
        name = nil;
    }), <DEmployMO: 0xd46e810> (entity: Employ; id: 0xd477310 <x-
    coredata:///DeductHistory/t19DEADD5-1BC1-4D3B-B42F-B5550628E307> ;
    data: {
        department = 0xd476b80 <x-coredata:///Department/
    tFCBB6254-2E5D-477E-B9B8-4B12D33C37B4>;
        name = nil;
    }), <EmployMO: 0xd4770d0> (entity: Employ; id: 0xd477100 <x-
    coredata:///DeductHistory/tDE7F27BC-054E-40E6-8613-3A4520A6B19F> ;
    data: {
        department = 0xd476b80 <x-coredata:///Department/
    tFCBB6254-2E5D-477E-B9B8-4B12D33C37B4>;
        name = nil;
    }), <EmployMO: 0x3f16c0> (entity: Employ; id: 0xd475640 <x-
    coredata:///Employ/t7CDB6496-0E29-4ACB-97B0-BE76A7C8510B> ; data: {
        department = 0x3f74c0 <x-coredata:///Department/
    t65DD1C8B-658B-4E5C-A430-CD2C39F5AD94>;
        name = nil;
    }), <DepartmentMO: 0xd476530> (entity: Department; id: 0xd4765a0 <x-
    coredata:///Department/tF81EC534-9B7C-4566-BAA1-A37ABB657FDA> ; data: {
        employees = (0xd4768e0 <x-coredata:///Employ/
    t9FA3775A-645C-4619-91E8-C885E04D7E32>);
        dept_name = "61-3?_U3000?_U30d7?_U30e9?_U30ba?_U30de?_U30c7
    ?_U30a3?_U30b9?_U30d7?_U30ec?_U30fc";
    }), <DepartmentMO: 0xd476b30> (entity: Department; id: 0xd476b80 <x-
    coredata:///Department/tFCBB6254-2E5D-477E-B9B8-4B12D33C37B4> ; data: {
        employees = (
            0xd476eb0 <x-coredata:///Employ/t2DE0457E-4138-4D02-AE71-
    BC40F31CD2D0>,
            0xd477400 <x-coredata:///Employ/tE7FC0BFB-DE93-4B60-B501-
    F8435C2E95C8>,
            0xd4777c0 <x-coredata:///Employ/tA08507B0-09F9-4B34-8E6C-
    EE72B38BA4F1>,
            0xd477100 <x-coredata:///Employ/
    tDE7F27BC-054E-40E6-8613-3A4520A6B19F>,
            0xd477310 <x-coredata:///Employ/t19DEADD5-1BC1-4D3B-B42F-
    B5550628E307>
        );
        dept_name = "?_U30c6?_U30b9?_U30c8";
    }), < EmployMO: 0xd477f10> (entity: Employ; id: 0xd477ef0 <x-
    coredata:///Employ/t3F17FB95-04A2-4344-A09A-7CEE58E31F1C> ; data: {
        department = 0xd477bc0 <x-coredata:///Department/
    t8EF2ED34-4D99-441A-9F9E-107AEB9DB962>;
        name = nil;
    }), <EmployMO: 0xd478470> (entity: Employ; id: 0xd4784a0 <x-
    coredata:///Employ/t5811C18C-F57F-4FB2-A0C4-AC34E119A99B> ; data: {
        department = 0xd477bc0 <x-coredata:///Department/
    t8EF2ED34-4D99-441A-9F9E-107AEB9DB962>;
        name = nil;
    }))

    Uli, thanks for your advice. I'll go see the URL and study more.

    Norio

    On 2007/10/01, at 1:52, Fritz Anderson wrote:

    >
    > On 30 Sep 2007, at 9:26 AM, Norio Ota wrote:
    >> An EXC_BAD_ACCESS error comes only after my app reads my custom
    >> format type of data and reads document nib file.
    >>
    >> 1. When new document is selected, no problem.
    >> 2. When open document is selected and the app reads one of three
    >> formats which Core Data supports, no problem.
    >> 3. Only after reading custom formatted file and after
    >> windowControllerDidLoadNib gets called, EXC_BAD_ACCESS comes.
    >> The windowControllerDidLoadNib method calls [super
    >> windowControllerDidLoadNib] and detects how each attribute of
    >> managedObjects
    >> has been set.
    >>
    >> It looks fine that the custom formatted data is read, and the
    >> objects created and initialized with the data seem to be fine.
    >> Because the value of each attribute of managedObjects was what I
    >> expected.
    >
    > You don't say what you learned from the debugger. Does any of your
    > code appear in the stack trace?
    >
    > ?\ F
    >
  • You've already received great tips from some seasoned vets, but I thought
    I'd toss another link at you:

    http://developer.apple.com/technotes/tn2004/tn2124.html#SECFOUNDATION

    By setting a few of those environment variables I've been able to track down
    pretty nasty autorelease bugs.

    Cheers,
    CxT
previous month september 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today