2009-07-11 27 views
2

我有數據對象類:爲什麼我的程序在自我訪問屬性時崩潰。和一個合成訪問器?

@interface Item: NSObject { 
    NSString *title; 
    NSString *text; 
} 

@property (copy) NSString *title; 
@property (copy) NSString *text; 

@end 

@implementation Item 

@synthesize text; 

- (void)updateText { 
    [email protected]"new text"; 
} 

- (NSString *)title { 
    return title; 
} 

- (void)setTitle:(NSString *)aString { 
    [title release]; 
    title = [aString copy]; 
} 

@end 

我可以使用非合成的方法時設置title屬性就好,但是當我設置一個屬性與合成訪問器我得到在上線updateText方法的錯誤讀取:

[email protected]"new text"; 

錯誤是:

*** NSInvocation: warning: object 0x462d2c0 of class '_NSZombie_CFString' does not implement methodSignatureForSelector: -- trouble ahead 
*** NSInvocation: warning: object 0x462d2c0 of class '_NSZombie_CFString' does not implement doesNotRecognizeSelector: -- abort 

爲什麼相同非合成的連接器工作和合成的不工作?

該對象在主線程中創建,並且從NSOperation線程訪問它時出現錯誤。

回答

0

你發佈的代碼對我來說工作得很好。這段代碼和你使用的實際代碼有什麼不同?

您看到的錯誤消息引用了「殭屍」,它們是已釋放對象的指針。這段代碼沒有顯示出任何這種行爲的風險,這導致我認爲實際的錯誤在別處。

一個可能的解決方案是使用Xcode的調試器查看NSString對象的地址,並使用該信息確定哪一個最終導致NSInvocation警告。

+0

該對象在主線程中創建,並且從NSOperation線程訪問它時出現錯誤。 – Rod 2009-07-11 14:18:06

3

的制定者應編碼是這樣的:

[title autorelease] 
title = [aString copy]; 

否則另一個線程可能會踩在腳下發布了標題對象。

或從Memory Management Programming Guide for Cocoa

+2

有沒有必要浪費做這個autorelease。一個二傳手的標準代碼只是防止新舊標題的指針相等。 if(title!= aString){[title release]; title = [aString copy]; } – 2009-07-12 03:01:53

0

任何其他一致的訪問風格在此代碼[self setTitle:[self title]]會釋放和dealloc的title複製之前。 您需要檢查setter中是否有title == aString

相關問題