I am working on a project which is non-ARC. The project has a singleton class which is use like a global functions class.
Everything works fine. Except for the following problems:
- Added a class with ARC
- When the singleton class is accessed from the ARC based class, it works for the first time
- Probably it is releasing the singleton class and further calls to the singleton class crashes the app with message “message sent to de-allocated instance”
I can imagine that the ARC enabled class is kind of releasing the singleton object.
How can i overcome this?
Edit: Singleton Class initializer GlobalFunctions.m
#import "GlobalFunctions.h"
#import <CoreData/CoreData.h>
#import "UIImage+Tint.h"
#import "Reachability.h"
#if !TARGET_IPHONE_SIMULATOR
#define Type @"Device"
#else
#define Type @"Simulator"
#endif
@implementation GlobalFunctions
#pragma mark {Synthesize}
@synthesize firstLaunch=_firstLaunch;
@synthesize context = _context;
#pragma mark {Initializer}
static GlobalFunctions *sharedGlobalFunctions=nil;
- (UIColor *)UIColorFromRGB:(NSInteger)red:(NSInteger)green:(NSInteger) blue {
CGFloat nRed=red/255.0;
CGFloat nBlue=green/255.0;
CGFloat nGreen=blue/255.0;
return [[[UIColor alloc]initWithRed:nRed green:nBlue blue:nGreen alpha:1] autorelease];
}
#pragma mark {Class Intialization}
+(GlobalFunctions *)sharedGlobalFunctions{
if(sharedGlobalFunctions==nil){
// sharedGlobalFunctions=[[super allocWithZone:NULL] init];
sharedGlobalFunctions=[[GlobalFunctions alloc] init]; //Stack Overflow recommendation, does'nt work
// Custom initialization
/*
Variable Initialization and checks
*/
sharedGlobalFunctions.firstLaunch=@"YES";
id appDelegate=(id)[[UIApplication sharedApplication] delegate];
sharedGlobalFunctions.context=[appDelegate managedObjectContext];
}
return sharedGlobalFunctions;
}
-(id)copyWithZone:(NSZone *)zone{
return self;
}
-(id)retain{
return self;
}
-(NSUInteger) retainCount{
return NSUIntegerMax;
}
-(void) dealloc{
[super dealloc];
[_context release];
}
@end
GlobalFunctions.h
#import <Foundation/Foundation.h>
@interface GlobalFunctions : NSObject<UIApplicationDelegate>{
NSString *firstLaunch;
}
+(GlobalFunctions *)sharedGlobalFunctions; //Shared Object
#pragma mark {Function Declarations}
-(UIColor *)UIColorFromRGB:(NSInteger)red:(NSInteger)green:(NSInteger) blue; // Convert color to RGB
#pragma mark {Database Objects}
@property (nonatomic,retain) NSManagedObjectContext *context;
@end
Edit:
Tried using [[GlobalFunctions alloc] init] as Anshu suggested. But still the app crashes with message “sent to deallocated instance”
First, remove the
copyWithZone:,retainandretainCountmethods; they are useless in a singleton.Secondly, that
deallocmethod is wrong;[super dealloc]must always be the last statement.The problem is your singleton itself; you override
retainto do nothing, but don’t overriderelease. The ARC’d class is likely callingretainat the beginning of a scope andreleaseat the end. Since the singleton’sreleasestill actually decrements the retain count, the singleton is deallocated.Remove the various methods as mentioned above and it should just work.
Note that your
GlobalFunctionsclass shouldn’t be declared as implementing<UIApplicationDelegate>as it is not the app’s delegate. Also, having two means of grabbing the same managed object context is odd (but not fatal).