简体   繁体   English

Singleton在具有ARC和非ARC的项目中起作用?

[英]Singleton functions in a project with ARC and non-ARC?

I am working on a project which is non-ARC. 我正在从事非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 用ARC添加了一个类
  • When the singleton class is accessed from the ARC based class, it works for the first time 当从基于ARC的类访问单例类时,它第一次起作用
  • 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. 我可以想象启用了ARC的类可以释放单例对象。

How can i overcome this? 我该如何克服呢?

Edit: Singleton Class initializer GlobalFunctions.m 编辑:Singleton类初始化程序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 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. 如Anshu建议使用[[GlobalFunctions alloc] init]进行了尝试。 But still the app crashes with message "sent to deallocated instance" 但是应用仍然崩溃,并显示消息“已发送到已释放实例”

First, remove the copyWithZone: , retain and retainCount methods; 首先,删除copyWithZone: retainretainCount方法; they are useless in a singleton. 他们在单例中毫无用处。

Secondly, that dealloc method is wrong; 其次,那个dealloc方法是错误的。 [super dealloc] must always be the last statement . [super dealloc] 必须始终是最后一个语句

The problem is your singleton itself; 问题出在你的单身汉身上。 you override retain to do nothing, but don't override release . 您覆盖了retain不执行任何操作,但不覆盖了release The ARC'd class is likely calling retain at the beginning of a scope and release at the end. ARC的类很可能在作用域的开头调用“ retain ,在结尾处调用“ release ”。 Since the singleton's release still actually decrements the retain count, the singleton is deallocated. 由于单例的release实际上仍会减少保留计数,因此将单例释放。

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. 请注意,不应将您的GlobalFunctions类声明为实现<UIApplicationDelegate>因为它不是应用程序的委托。 Also, having two means of grabbing the same managed object context is odd (but not fatal). 同样,用两种方法来获取相同的管理对象上下文是奇怪的(但不是致命的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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