IOS singleton Crash Analysis 2014-12-10 15:46:36

Singleton pattern is a common pattern, but crash initiator or even in a single embodiment ridiculous applications. Is really a single case caused it? not necessarily. But the phenomenon are pointing to a single case caused. Today, I experienced an example of a seemingly crashes on a single case, but actually is not, today, to be a record for the analysis of similar problems in the future.

I have an application using the Singleton pattern to maintain network monitoring state. KVO mode uses to listen for  changes networkStateInstance, the listener's own custom implementation  observeValueForKeyPath method. Define code is as follows:

@interface SNNetState : NSObject

 // current network status

@property (nonatomic, assign) SNNetWorkStates networkStateInstance;

+ (SNNetState *)intance;

@end

Implementation as follows:

+ (SNNetState *)intance {

    static SNNetState* _intance = nil;

    static dispatch_once_t netStateOnece;

    dispatch_once(&netStateOnece, ^{

        _intance = [[SNNetState alloc] init];

    });

    

    return _intance;

}

IOS singleton crash analysis - Nighthawk - Nighthawk

 

Look phenomenon is caused by a single case of problems, but the background has informed us really want:IOS singleton crash analysis - Nighthawk - Nighthawk

 

IMsViewController listener objects have been released, and when our time networkStateInstance change IMsViewController the object does not exist led to the collapse. So we have wronged a single case, but a lot of people stubbornly rigid thought to be caused by a single example. In this there is a commonly used method can help you to accurately cause of the problem. Select Product in the xcode - "Scheme-" Edit Scheme pop-up edit box as shown in settings: IOS singleton crash analysis - Nighthawk - Nighthawkyou can easily locate the cause of the problem.

Guess you like

Origin www.cnblogs.com/lu-ping-yin/p/10992768.html