简体   繁体   中英

Singleton functions in a project with ARC and non-ARC?

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: , retain and retainCount methods; they are useless in a singleton.

Secondly, that dealloc method is wrong; [super dealloc] must always be the last statement .

The problem is your singleton itself; you override retain to do nothing, but don't override release . The ARC'd class is likely calling retain at the beginning of a scope and release at the end. Since the singleton's release still actually decrements the retain count, the singleton is deallocated.

Remove the various methods as mentioned above and it should just work.

Note that your GlobalFunctions class 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM