Standard Cocoa Save Panel crashing in Leopard

  • I posted messages recently about frequent crashes in standard open and
    save panels (in Leopard only) and was assured it must be bug in my
    code. The crash log looked like it crashed on a message involving
    NSTextStorage and indeed I found an NSTextStorage object I was using
    to format labels for a graphic view. I was releasing that object's
    layout manager, but not the object. That sounded like the problem. I
    fixed it and the crashing stopped for a couple of days. Today it
    crashed again during a simple save as panel and the crash log (see
    below) still mentions NSTextStorage. I looked again, but can not find
    any more NSTextStorage objects in my code.

    I have run with NSZombies enabled, but the crashes are rare and thus
    hard to catch. Prior to the fix of my NSTextStorage object bug I got

    2007-12-04 16:56:44.131 NairnFEAMPM[27778:10b] *** -[NSLayoutManager
    textStorage:edited:range:changeInLength:invalidatedRange:]: message
    sent to deallocated instance 0x167c9420

    which also looked like my NSTextStorage object.

    Here is my "fixed" code to create an NSTextObject:

        textStorage = [[NSTextStorage alloc] initWithString:@"Hello"];
        layoutManager = [[NSLayoutManager alloc] init];
        textContainer = [[NSTextContainer alloc] init];
        [layoutManager addTextContainer:textContainer];
        [textContainer release];    // The layoutManager will retain
        [textStorage addLayoutManager:layoutManager];
        [layoutManager setUsesScreenFonts:NO];
        [layoutManager release];    // The textStorage will retain

    Then when the object is deallocated, I call [textStorage release].

    Here is piece of recent crash log which still looks like an
    NSTextStorage problem:

    Process:        NairnFEAMPM [37688]
    Path:            /Users/jnairn/Programming/Cocoa_Projects/NairnFEAMPM/
    build/Default/NairnFEAMPM.app/Contents/MacOS/NairnFEAMPM
    Identifier:      com.geditcom.NairnFEAMPM
    Version:        Version 1.6 (build 1)
    Code Type:      X86 (Native)
    Parent Process:  launchd [37563]

    Date/Time:      2007-12-21 11:17:26.308 -0800
    OS Version:      Mac OS X 10.5.1 (9B18)
    Report Version:  6

    Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
    Exception Codes: KERN_INVALID_ADDRESS at 0x00000000d027ef9b
    Crashed Thread:  0

    Thread 0 Crashed:
    0  libobjc.A.dylib                   0x9650c695 _class_isInitialized + 14
    1  libobjc.A.dylib                   0x9650248a
    _class_lookupMethodAndLoadCache + 66
    2  libobjc.A.dylib                   0x96512736 objc_msgSend + 102
    3  com.apple.AppKit                 0x91de4834 -[NSTextStorage
    fontSetChanged] + 363
    4  com.apple.CoreFoundation         0x96b97da5 -[NSArray
    makeObjectsPerformSelector:] + 565
    5  com.apple.AppKit                 0x91de4661 +[NSTextStorage
    _fontSetChanged:] + 228
    6  com.apple.Foundation             0x918ad67a _nsnote_callback + 106
    7  com.apple.CoreFoundation         0x96b0aa4a __CFXNotificationPost +
    362
    8  com.apple.CoreFoundation         0x96b0ad23
    _CFXNotificationPostNotification + 179
    9  com.apple.Foundation             0x918aaa70 -[NSNotificationCenter
    postNotificationName:object:userInfo:] + 128
    10  com.apple.Foundation             0x918b4108 -[NSNotificationCenter
    postNotificationName:object:] + 56
    11  com.apple.AppKit                 0x92009e29 +[NSFont
    _postFontSetChangedNotifications] + 75
    12  com.apple.Foundation             0x918aebfc __NSThreadPerformPerform
    + 476
    13  com.apple.CoreFoundation         0x96b29698 CFRunLoopRunSpecific +
    3240
    14  com.apple.CoreFoundation         0x96b29d38 CFRunLoopRunInMode + 88
    15  com.apple.HIToolbox               0x9003b8a4
    RunCurrentEventLoopInMode + 283
    16  com.apple.HIToolbox               0x9003b5f6 ReceiveNextEventCommon +
    175
    17  com.apple.HIToolbox               0x9003b531
    BlockUntilNextEventMatchingListInMode + 106
    18  com.apple.AppKit                 0x91bacd5b _DPSNextEvent + 657
    19  com.apple.AppKit                 0x91bac6a0 -[NSApplication
    nextEventMatchingMask:untilDate:inMode:dequeue:] + 128
    20  com.apple.AppKit                 0x91d5d29c -[NSApplication
    _shouldTerminate] + 529
    21  com.apple.AppKit                 0x91d5ce6e -[NSApplication
    terminate:] + 274
    22  com.apple.AppKit                 0x91c7df94 -[NSApplication
    sendAction:to:from:] + 112
    23  com.apple.AppKit                 0x91d2c868 -[NSMenu
    performActionForItemAtIndex:] + 493
    24  com.apple.AppKit                 0x91d2c56d -[NSCarbonMenuImpl
    performActionWithHighlightingForItemAtIndex:] + 220
    25  com.apple.AppKit                 0x91d2c1f2 -[NSMenu
    performKeyEquivalent:] + 866
    26  com.apple.AppKit                 0x91d2aa99 -[NSApplication
    _handleKeyEquivalent:] + 492
    27  com.apple.AppKit                 0x91c47e4e -[NSApplication
    sendEvent:] + 3824
    28  com.apple.AppKit                 0x91ba5705 -[NSApplication run] + 847
    29  com.apple.AppKit                 0x91b729ba NSApplicationMain + 574
    30  com.geditcom.NairnFEAMPM         0x00002b3c _start + 210
    31  com.geditcom.NairnFEAMPM         0x00002a69 start + 41

    ---------------
    John Nairn (1-541-737-4265, FAX:1-541-737-3385)
    Professor and Richardson Chair
    Web Page: http://woodscience.oregonstate.edu/faculty/Nairn
    FEA/MPM Web Page: http://oregonstate.edu/~nairnj
  • On 21 Dec 2007, at 4:21 PM, John Nairn wrote:

    > textContainer = [[NSTextContainer alloc] init];

    Perhaps it is not the source of your problem, but I wonder if -init is
    a proper initializer for NSTextContainer. The designated initializer
    is initWithContainerSize:.

    — F
  • Thanks Fritz. I was a little fuzzy on why I even wrote the text
    container code as I did, but then remembered it came directly from
    this Apple documention:

    "To use NSLayoutManager to draw a text string directly into a view,
    you must create and initialize the three basic nonview components of
    the text system. First create an NSTextStorage object to hold the
    string. Then create an NSTextContainer object to describe the
    geometric area for the text. Then create the NSLayoutManager object
    and hook the three objects together by adding the layout manager to
    the text storage object and adding the text container to the layout
    manager. The code in Listing 1, which could reside in the view’s
    initWithFrame: method, illustrates this procedure.

          NSTextStorage *textStorage = [[NSTextStorage alloc]
    initWithString:@"This is the text string."];
          NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
          NSTextContainer *textContainer = [[NSTextContainer alloc] init];
          [layoutManager addTextContainer:textContainer];
          [textContainer release];
          [textStorage addLayoutManager:layoutManager];
          [layoutManager release];

    You can release the text container because the layout manager retains
    it, and you can release the layout manager because the text storage
    object retains it."

    An this NSView works fine and displays the text as expected, I think
    the initialization is OK. I still suspect it, however, in the save
    panel crashes because they occur when the layout manager sends a
    message to a deallocated NSTextStorage object and this is the  only
    text storage object in my application? My object retains it an
    releases it. I have not subclassed it so I assume the Cocoa
    implementation properly removes itself from notifications when it is
    deallocated.

    On Dec 21, 2007, at 7:00 PM, Fritz Anderson wrote:

    > On 21 Dec 2007, at 4:21 PM, John Nairn wrote:
    >
    >> textContainer = [[NSTextContainer alloc] init];
    >
    > Perhaps it is not the source of your problem, but I wonder if -init
    > is a proper initializer for NSTextContainer. The designated
    > initializer is initWithContainerSize:.
    >
    > — F

    >> I posted messages recently about frequent crashes in standard open
    >> and save panels (in Leopard only) and was assured it must be bug in
    >> my code. The crash log looked like it crashed on a message
    >> involving NSTextStorage and indeed I found an NSTextStorage object
    >> I was using to format labels for a graphic view. I was releasing
    >> that object's layout manager, but not the object. That sounded like
    >> the problem. I fixed it and the crashing stopped for a couple of
    >> days. Today it crashed again during a simple save as panel and the
    >> crash log (see below) still mentions NSTextStorage. I looked again,
    >> but can not find any more NSTextStorage objects in my code.
    >>
    >> I have run with NSZombies enabled, but the crashes are rare and
    >> thus hard to catch. Prior to the fix of my NSTextStorage object bug
    >> I got
    >>
    >> 2007-12-04 16:56:44.131 NairnFEAMPM[27778:10b] *** -
    >> [NSLayoutManager
    >> textStorage:edited:range:changeInLength:invalidatedRange:]: message
    >> sent to deallocated instance 0x167c9420
    >>
    >
    ---------------
    John Nairn (1-541-737-4265, FAX:1-541-737-3385)
    Professor and Richardson Chair
    Web Page: http://woodscience.oregonstate.edu/faculty/Nairn
    FEA/MPM Web Page: http://oregonstate.edu/~nairnj
previous month december 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
31            
Go to today