2012-07-19 2 views
0

저는 ARC가 아닌 프로젝트를 진행하고 있습니다. 프로젝트에는 전역 함수 클래스처럼 사용되는 단일 클래스가 있습니다.싱글 톤은 ARC 및 비 ARC가있는 프로젝트에서 작동합니까?

모두 정상적으로 작동합니다. 다음과 같은 문제를 제외하고 :

  • 추가 ARC와 클래스 싱글 톤 클래스가 ARC 기반 클래스에서 액세스 할 때,
  • 이 아마 싱글 클래스와 더 방출되어 처음 작동
  • 싱글 톤 클래스에 대한 호출은 "메시지가 할당되지 않은 인스턴스로 전송 됨"메시지와 함께 응용 프로그램을 무효화합니다.

내가 알 수 있듯이 ARC 가능 클래스는 싱글 톤 객체를 방출하는 것으로 생각됩니다.

어떻게이 문제를 해결할 수 있습니까?

편집 : 싱글 클래스 초기화 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 
     */ 
     [email protected]"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 

편집 : Anshu이 제안 [초기화 [GlobalFunctions의 ALLOC]]를 사용하여 시도

. 하지만 여전히 앱이 "할당 취소 된 인스턴스로 전송되었습니다"라는 메시지와 충돌합니다.

+2

싱글 톤을 어떻게 구현 했습니까? 객체에 대한 강력한 참조가있는 한 ARC가 무작위로 객체를 릴리스해야하는 이유는 없습니다. 싱글 톤 구현에 문제가있는 것처럼 들립니다. –

+0

안녕하세요 Anshu, 내 싱글 톤 클래스의 이니셜 라이저를 추가했습니다. 맞지 않다고 확신합니까? 그렇지? – Veeru

+0

여기서'allocWithZone :'을 사용하고 싶지 않습니다. [ARC release notes] (http://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html)에서는 어쨌든 메모리 영역을 더 이상 사용할 수 없다고 말합니다. '[[GlobalFunctions alloc] init]'만 시도해보십시오. –

답변

4

먼저 copyWithZone:,,및 retainCount 메서드를 제거합니다. 그들은 싱글 톤에서는 쓸모가 없다.

두 번째로는 dealloc 방법이 잘못되었습니다. [super dealloc]은 항상 마지막 문인이어야합니다.

문제는 싱글 톤 자체입니다. retain을 무시하고 아무 것도하지 않고 release을 재정의하지 마십시오. ARC 클래스는 범위 시작 부분에 retain을 호출하고 마지막에는 release을 호출 할 가능성이 큽니다. 싱글 톤의 release이 여전히 보유 카운트를 실제로 감소시키기 때문에, 싱글 톤은 할당이 해제됩니다.

위에서 언급 한 다양한 방법을 제거하면 효과가 있습니다.

GlobalFunctions 클래스는 앱의 위임자가 아니기 때문에 <UIApplicationDelegate>을 구현하는 것으로 선언하면 안됩니다. 또한 동일한 관리 객체 컨텍스트를 잡는 두 가지 방법은 홀수입니다 (그러나 치명적이지는 않습니다).

+0

그건 분명히 효과가 있었고, 많은 것을 배웠습니다. 나는 당신이 언급 한 것들을 묘사하는 적절한 자료를 찾는다. 추천 할 수있는 책이나 자료가 있습니까? – Veeru

+0

나는 몇 년 전에 Apple (다음 NeXT)에서 제공 한 문서를 참고했다. 요즘 어떤 책을 권할 지 모르겠습니다. – bbum

관련 문제