There is a strange behavior in my code and I really don’t know how to solve it.
I have a Singleton class with this definition:
AppModelLocator.h>
#import <Foundation/Foundation.h>
@interface AppModelManager : NSObject
+ (AppModelManager *)sharedManager;
@end
AppModelLocator.m
#import "AppModelManager.h"
static AppModelManager *instance = nil;
@implementation AppModelManager
#pragma mark - Singletone
#pragma mark
+ (AppModelManager *)sharedManager
{
@synchronized ([AppModelManager class]) {
if (instance == nil) {
instance = [AppModelManager new];
}
}
return instance;
}
+ (id)alloc
{
@synchronized ([AppModelManager class]) {
NSAssert(instance == nil, @"Attempted to allocate the second instance of AppModelManager.");
instance = [super alloc];
return instance;
}
return nil;
}
@end
When I call [AppModelLocator sharedManager]somewhere in my code everything is fine. But when I call the singleton class after a specific line of code it throws me EXC_BAD_ACCESS (code=1, address=0xfffffeec) and refers to return instance in sharedManager definition in the singleton class.
That specific code is initializing a class that create a HTTP request and start sending the request but in the class is not any reference of AppModelLocator or something special. It is a simple creating of NSURLConnection and its delegate methods.
I used similar classes and approach in other applications and they are working fine and I wonder what is wrong with this class. I tried a dozen other ways of creating singleton class but none of them were useful.
Finally, I found what was wrong in the code. I made a
NSURLinstance dynamically and I used 3rd library to encrypt the URL. Something through the encoding caused the crash in special condition but I wonder why the exception raised after calling the singleton class. Anyway the issue was not because of the singleton definition as deanWombourne said.