2011-12-23 28 views
0

我來自更多的Android開發背景,所以如果這是一個愚蠢的問題,請道歉,但這只是令我頭腦發呆,而我看不出什麼是錯的。我有一個Singleton類實現如下:使用Singleton類更改選項卡會導致SIGABRT

頭文件

@interface SingletonClass : NSObject 
{ 
} 

@property(nonatomic, retain) NSMutableArray *categoryArray; 

+ (SingletonClass *)sharedInstance; 
- (id) init; 
- (void)setCategory: (NSMutableArray *) x; 
- (NSMutableArray *)getCategory; 

@end 

類實現:

#import "SingletonClass.h" 

@implementation SingletonClass 
@synthesize categoryArray; 


static SingletonClass *sharedInstance = nil; 

+ (SingletonClass *)sharedInstance { 
    if (sharedInstance == nil) { 
     sharedInstance = [[super allocWithZone:NULL] init]; 
    } 
    return sharedInstance; 
} 

- (id)init 
{ 
    self = [super init]; 

    if (self) { 
     categoryArray = [[NSMutableArray alloc] init]; 
    } 

    return self; 
} 

+ (id)allocWithZone:(NSZone*)zone { 
    return [self sharedInstance]; 
} 

- (id)copyWithZone:(NSZone *)zone { 
    return self; 
} 

- (void)setCategory: (NSMutableArray *)category_array{ 
    categoryArray = category_array; 
} 

- (NSMutableArray *)getCategory{ 
    return categoryArray; 
} 

@end 

我有2個選項卡每個我試圖訪問其持有的Singleton對象我需要使用的陣列:

SingletonClass* myapp = [SingletonClass sharedInstance]; 
categories = [myapp getCategory]; 

當切換選項卡時它工作時單工對象沒有被調用,但一旦我使用它,我得到SIGABRT錯誤。 (認爲​​這是一個內存警告)。單標籤實例是否不能跨標籤共享?

回答

0

是的,單身人士當然可以跨標籤分享。

a)如果您沒有使用ARC,那麼您的引用計數是關閉的。 b)這聽起來像一個引用計數偏移量(可能發生在ARC,MRC或GC)。有一個診斷模式「啓用殭屍」。基本上,這意味着對象實際上並未被釋放,並且當它們的引用計數達到零時被「zombified」。 zombified對象會在發送消息時注意到一個錯誤。您可以使用此工具並在樂器中使用殭屍工具記錄對象的引用計數。

+0

感謝您的幫助。我正在使用ARC,所以只專注於你的b點,我已經嘗試殭屍儀器與殭屍啓用,但沒有被標記我只是得到相同的錯誤,沒有控制檯輸出它只是說gbd,XCode跳轉到main.m類並顯示SIGABRT錯誤。 – SamRowley 2011-12-23 15:18:11

0

你能從調用Singleton的方法中顯示更多代碼嗎? 還有一些更多的控制檯輸出會有幫助。

我的猜測是,category_Array是一個懸掛指針,原來的categoryArray已經被釋放。

保留setter(「setCategory:」方法)應保留它的新值並釋放舊值。

+0

這基本上是用於調用Singleton的方法,並且沒有控制檯輸出只有gbd。當我嘗試更改選項卡XCode跳轉到main.m類並顯示SIGABRT錯誤。我正在使用ARC,因此無法在創建的對象上調用retain。感謝您的幫助。 – SamRowley 2011-12-23 14:49:10

+0

啊,當你在房產中使用保留而不是堅強的時候,我很惱火。 所以我認爲你不會使用ARC。 – Thyraz 2011-12-23 16:02:32

+0

哦,這是我的不好,我仍然得到和以前一樣的問題,即使它變成了強壯而不是保留。 – SamRowley 2011-12-23 16:21:59

0

只想對那些試圖幫助我的人說聲謝謝,我遇到的問題實在是微不足道。我有使用故事板創建的應用程序流,並有錯誤的CellIdentifier集,因此會引發內存錯誤。

相關問題