EXC_BAD_ACCESS help

  • I've got an EXC_BAD_ACCESS error on line #2 below.

    // retainCount = 1

    NSLog(@"patternCount retainCount = %d", [patternCount retainCount]);

    // EXC_BAD_ACCESS on this line ...

    NSLog(@"patternCount = %d", [patternCount intValue]);

    patternCount is of type NSNumber* which is held inside my DataPattern object
    that I've sent over YAMessageQueue (async from one thread to the main gui
    thread) to a controller.

    @interface DataPattern : NSObject {

    ...

    NSNumber *patternCount;

    NSNumber *otherData;

    }

    I believe I've got my memory management correct and that my retain counts
    are good.

    I've successfully sent hundreds of objects from a thread to my app's main
    thread over YAMessageQueue asynchronously (and vice versa), but then every
    once in a while my app crashes. Although it could be any member of my
    DataPattern object that shows the EXC_BAD_ACCESS problem, the other members
    of that object are fine when I debug them.

    Anyone have an idea?
  • > I believe I've got my memory management correct and that my retain
    > counts
    > are good.
    >
      DO. NOT. RELY. ON. RETAIN. COUNT.

      I say that so boldly so there is no chance of being unclear. If
    you're blindly basing "correct memory management" on retain count, you
    do not yet understand the memory management mechanism. An object
    instance's retain count is, frankly, none of your business. That's
    between the instance and the system.

    > Anyone have an idea?

      Yes, a bright one. Read this thoroughly:

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


      Does the entire life cycle of your problem instance follow these
    rules? If your answer is no, there's your problem. If your answer is
    yes, I call bull****. ;-)

    --
    I.S.
  • I really think I understand that link, and the only reason I bring up the
    retain count is because I know I'm not releasing it too early (cause I don't
    release these objects for now).
    What I'm afraid is happening is that it's being overwritten somehow and that
    when I access the NSNumber object, I get the error. Or possibly something to
    do with the threads that I don't understand, but I just don't see it.

    On Jan 20, 2008 12:09 AM, I. Savant <idiotsavant2005...> wrote:

    >> I believe I've got my memory management correct and that my retain
    >> counts
    >> are good.
    >>
    > DO. NOT. RELY. ON. RETAIN. COUNT.
    >
    > I say that so boldly so there is no chance of being unclear. If
    > you're blindly basing "correct memory management" on retain count, you
    > do not yet understand the memory management mechanism. An object
    > instance's retain count is, frankly, none of your business. That's
    > between the instance and the system.
    >
    >> Anyone have an idea?
    >
    > Yes, a bright one. Read this thoroughly:
    >
    >
    > http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Tasks/
    MemoryManagementRules.html

    >
    > Does the entire life cycle of your problem instance follow these
    > rules? If your answer is no, there's your problem. If your answer is
    > yes, I call bull****. ;-)
    >
    > --
    > I.S.
    >
    >
    >
    >
  • On Jan 19, 2008, at 10:20 PM, William Zumwalt wrote:
    > I really think I understand that link, and the only reason I bring
    > up the
    > retain count is because I know I'm not releasing it too early (cause
    > I don't
    > release these objects for now).
    > What I'm afraid is happening is that it's being overwritten somehow
    > and that
    > when I access the NSNumber object, I get the error. Or possibly
    > something to
    > do with the threads that I don't understand, but I just don't see it.

    I. Savant is correct in his negativity towards -retainCount.  Many a
    time, I have wished that method went the way of the dodo in about '97
    or so...

    But, given that -retainCount "works" and -intValue does not, your
    assessment that something is munching on memory is quite likely correct.

    Was the code in the original email quite literally a copy/paste --
    i.e. was the invocation of -retainCount and -intValue that adjacent?

    Have you tried debugging with NSZombieEnabled?

    You mention threads -- NSValues are immutable and, thus, interaction
    with threads is quite typically along the lines of incorrectly
    managing retain/release as the object is transferred between threads.

    b.bum
  • Is it autoreleased in thread A, then accessed in thread B? If so, then
    thread A could dealloc it before thread B accesses it.

    --
    Scott Ribe
    <scott_ribe...>
    http://www.killerbytes.com/
    (303) 722-0567 voice
previous month january 2008 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 31      
Go to today