views:

17

answers:

0

Hi,

I've got a really weird crash on Leopard (not on Snow Leopard) concerning NSLayoutManager, NSTextView and the background layout feature.

My application is 64-bit garbage collected and that seems to be the root of the problem. I've established that the crash happens like this:

1) the system decides to do some background layout on some NSTextView and puts the background layout request onto the run loop

2) the garbage collector comes and disposes of at least the NSTextStorage and possibly (even though I can't be sure about this) the NSTextView and NSLayoutManager; it is possible that those are not "collected" because of the reference on the run loop.

3) the run loop dequeues the request and starts doing some background layout, the layout manager crashes as soon as it accesses the no longer existing NSTextStorage

Now this is weird because you'd think that the run loop request would still hold a reference to the layout manager, which would have a reference to the text storage, which would have a reference to the text view, etc.. under Snow Leopard none of this seems to go awry so I'm fairly certain it's a bug in Leopard.

My simple minded workaround was to switch off background layout for all NSTextViews that I create.. and that appears to be working as there are fewer crashes.

I use:

  [NSLayoutManager setBackgroundLayoutEnabled: NO]

but I still get identical crashes. More exploration has found that NSTextViews are also allocated as "field editors" automatically by the system as soon as you've got a text field of any description in the window. I've overloaded NSWindow's:

  - (NSText *)fieldEditor:(BOOL)createWhenNeeded forObject:(id)anObject;

and return the instance returned by the super class with a layout manager that has had its background layout switched off.

Still.. I get the same crashes again. Of course never on my machines.. and even the two people who have reported the problem only get a crash every other week or so.

There must be somewhere else where NSLayoutManagers are allocated automatically by the system, since all the field editors and all the layout managers created by my program have their background layout switched off.. so why do I still get crashes in the background layout!?

Any brain waves would be much appreciated.

Best regards,

Frank