Re: KVO can be unsafe in -init?

2009-09-13 Thread Uli Kusterer
Am 09.09.2009 um 11:02 schrieb John Chang: No, it's a singleton. Basically it's a manager object that wants to continually monitor something else for changes. Also, - addObserver is the last line of code in -init, before the return. This article may be of interest to you:

Re: KVO can be unsafe in -init?

2009-09-13 Thread John Chang
On 13 sep 2009, at 05.33, Jerry Krinock wrote: I'd be interested to know how John is going to fix this problem. I myself have used the performSelector:withObject:afterDelay:0.0 solution in situations like this I think the problem really only happens with the

Re: KVO can be unsafe in -init?

2009-09-12 Thread Jerry Krinock
On 2009 Sep 11, at 05:55, John Chang wrote: Just to update you guys. We managed to reproduce the problem in- house and catch it in the debugger. I'd be interested to know how John is going to fix this problem. I myself have used the performSelector:withObject:afterDelay:0.0 solution in

Re: KVO can be unsafe in -init?

2009-09-11 Thread John Chang
Just to update you guys. We managed to reproduce the problem in-house and catch it in the debugger. The backtrace looks the same as the one in the auto-generated crash reports. (gdb) bt #0 0x02546004 in ___TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION___ () #1 0x98c81f49 in objc_exception_throw () #2

Re: KVO can be unsafe in -init?

2009-09-09 Thread Ben Trumbull
On 08/09/2009, at 12:36 AM, John Chang wrote: Hi all, Question: is it unsafe for some reason to be adding yourself as a KVO observer during -init? We have a singleton with an -init that looks something like this: - (id)init { if ((self = [super init])) { _foo = [[NSMutableDictionary alloc]

Re: KVO can be unsafe in -init?

2009-09-09 Thread John Chang
On 9 sep 2009, at 04.51, Graham Cox wrote: Because you're trying to use a nil key with a dictionary. Why not set a breakpoint on objc_exception_throw and see where that is being attempted? Because it's happening for some users and not in-house :) On 9 sep 2009, at 05.02, Ken Ferry

Re: KVO can be unsafe in -init?

2009-09-09 Thread John Chang
On 9 sep 2009, at 11.21, Mike Abdullah wrote: Well I would question why you're calling +[XYZManager sharedManager] from within -[XYZManager init] like that. What if you change your init routine to: Oops, sorry. It's actually two different classes which I renamed incorrectly for the

Re: KVO can be unsafe in -init?

2009-09-09 Thread Mike Abdullah
On 9 Sep 2009, at 10:31, John Chang wrote: On 9 sep 2009, at 11.21, Mike Abdullah wrote: Well I would question why you're calling +[XYZManager sharedManager] from within -[XYZManager init] like that. What if you change your init routine to: Oops, sorry. It's actually two different

Re: KVO can be unsafe in -init?

2009-09-09 Thread Roland King
On 09-Sep-2009, at 5:02 PM, John Chang wrote: On 9 sep 2009, at 05.03, Roland King wrote: One thing to note, you're using a context of NULL, which I used to do all the time until I got badly bitten. What would you recommend using as the context? I'm used to checking object/keyPath,

Re: KVO can be unsafe in -init?

2009-09-09 Thread Dave Keck
What do I use. Normally a class static, provided that's granular enough, always has been for me thus far. Either just make some static thing like a character array and take the address of it or, as I've been doing recently, use the address of the class object which I grab in a +initialize()

Re: KVO can be unsafe in -init?

2009-09-09 Thread Keith Duncan
On 9 Sep 2009, at 11:21, Dave Keck wrote: static void *const MyClass_KVOContext = (void *)MyClass_KVOContext; That certainly is unusual, I use the following macro: #define NSSTRING_CONTEXT(var) static NSString *var = @#var then use the address of the identifier passed as var. This has the

Re: KVO can be unsafe in -init?

2009-09-09 Thread Marc Respass
We have a singleton with an -init that looks something like this: - (id)init { if ((self = [super init])) { _foo = [[NSMutableDictionary alloc] init]; _bar = [[NSMutableDictionary alloc] init]; [[XYZManager sharedManager] addObserver:self forKeyPath:@allObjects

Re: KVO can be unsafe in -init?

2009-09-09 Thread Roland King
What about writing a method, -finishSetup, and invoke it with a delay like below? I use this pattern a lot with loading views. It seems like it would work so that you add observation when your object is ready. Marc [self performSelectorOnMainThread:@selector(finishSetup)

KVO can be unsafe in -init?

2009-09-08 Thread John Chang
Hi all, Question: is it unsafe for some reason to be adding yourself as a KVO observer during -init? We have a singleton with an -init that looks something like this: - (id)init { if ((self = [super init])) { _foo = [[NSMutableDictionary alloc] init]; _bar = [[NSMutableDictionary alloc] init];

Re: KVO can be unsafe in -init?

2009-09-08 Thread Roland King
John Chang wrote: Hi all, Question: is it unsafe for some reason to be adding yourself as a KVO observer during -init? We have a singleton with an -init that looks something like this: - (id)init { if ((self = [super init])) { _foo = [[NSMutableDictionary alloc] init]; _bar =

Re: KVO can be unsafe in -init?

2009-09-08 Thread Stephen J. Butler
On Mon, Sep 7, 2009 at 9:36 AM, John Changjohn.r.ch...@gmail.com wrote: This code is running on iPhone OS. On some devices (we haven't been to narrow this down), the last line of code is throwing an exception: Sun Sep  6 13:41:26 unknown MyApp[1609] Error: *** Terminating app due to uncaught

Re: KVO can be unsafe in -init?

2009-09-08 Thread Graham Cox
On 08/09/2009, at 12:36 AM, John Chang wrote: Hi all, Question: is it unsafe for some reason to be adding yourself as a KVO observer during -init? We have a singleton with an -init that looks something like this: - (id)init { if ((self = [super init])) { _foo = [[NSMutableDictionary alloc]

Re: KVO can be unsafe in -init?

2009-09-08 Thread Ken Ferry
Hi John, Since you're passing NSKeyValueObservingOptionInitial, -observeValueForKeyPath: is going to be invoked on your object immediately, before the method even returns. Is your -observeValueForKeyPath: safe to be called with a partially set up object? Do you have any subclasses of your