2013-01-23 39 views
-1

我有一個View對象在我的方法中突然變爲零。查看對象突然變爲零 - xcode

我沒有使用ARC

沒有線程參與


請告訴我發生的事情是一日一次我稱之爲1stmethod方法的一切工作正常,並於livescoreSettings參考被保留。

下一頁時,我打電話2ndmethod方法也livescoreSettings裁判保留,但由當時的委託方法被激活變量丟失的參考..不知道爲什麼...

@interface XY { 
    LiveScoreSettingsView * livescoreSettings; // initialisation in .h file inside  
} 
@end 

@implementation 

// 1st method 
- (void)1stmethod:(id) callingClass username:(NSString*)username { 
    livescoreSettings=callingClass; // retain count increases to 1 
    isLivescoresSettingsView = YES; 

    //.... some code where the above livescoreSettings variables are not used ... // 
} 

// 2nd method 
- (void)2ndmethod:(id) callingClass username:(NSString*)username matchid:(NSString *)matchid eventType:(NSString *) eventType add:(NSString *) add { 
    livescoreSettings=callingClass; 
    isLivescoresSettingsView = YES; 
    addEventToList = YES; 

    //.... some code where the above livescoreSettings variables are not used ... // 
} 

// delegate method thats activated when the response comes 
- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq { 
    // the block where the data is sent to a particular view to reload table 
    else if(isLivescoresSettingsView== YES || addEventToList == YES) { 
    isLivescoresSettingsView=NO; 
    addEventToList = NO; 

    //.... some code where the above livescoreSettings variables are not used ... // 

    if(success) 
     NSLog(@"No Errors with retain count = %d ", [livescoreSettings retainCount]); 
    else 
     NSLog(@"Error Error Error!!!"); 

    [livescoreSettings.tableView reloadData]; 

    // when **2ndmethod** is called there's no memory reference to livescoreSettings, tableView delegate methods are not called which is obvious. But not sure why the retain count is reducing abruptly. 
    } 
} 

@end 
+0

是否使用ARC? – trojanfoe

+0

也是'livescoreSettings'的一個實例變量? – trojanfoe

+0

沒有ARC ... livescoreSettings不是一個實例變量..它在.h文件中被聲明 –

回答

1

的問題是您沒有獲得或2ndmethodlivescoreSetting所有權。如果你不使用ARC,那麼你將需要retain它在這些方法和release它在你的dealloc方法(簡單地分配實例livescoreSetting確實而不是增加使用MRR時的保留計數)。

想像一下,如果1stmethod被稱爲以這種方式:

LivescoreSettingsView *view = [[LivescoreSettingsView alloc] init]; 
[whateverItsCalled 1stmethod:view;   // (1) 
[view release];       // (2) 

然後view被分配給在whateverItsCalled.livescoreSetting(1),但保留計數爲1。(2)在保持計數爲0之後,但是whateverItsCalled.livescoreSetting現在是一個懸掛的指針,我很驚訝你沒有看到像「消息發送到釋放對象」而不是你看到的錯誤(當ARC不參與時爲什麼它被分配到nil)。

要解決該問題,您需要爲實例變量設置synthesize您的setter/getter方法,方法是爲其添加@property。我更喜歡使用前導下劃線(_)來命名實例變量,以將它們與setter/getter方法名稱區分開來;所以:

.h文件中:

@interface WhateverItsCalled : NSObject 
{ 
    LiveScoreSettingsView *_livescoreSetting; 
} 

@property (retain, nonatomic, readwrite) LiveScoreSettingsView *livescoreSetting; 

.m文件:

@implementation WhateverItsCalled 
@synthesize livescoreSetting = _livescoreSetting; 

- (void)dealloc 
{ 
    self.livescoreSetting = nil;   // Release the object by assigning nil 
    [super dealloc]; 
} 

- (void)firstmethod:(id) callingClass username:(NSString*)username 
{ 
    self.livescoreSettings = callingClass; // Note the use of self! 
    isLivescoresSettingsView = YES; 
} 

- (void)secondmethod:(id)callingClass username:(NSString*)username matchid:(NSString *) matchid eventType:(NSString *) eventType add:(NSString *) add 

{ 
    self.livescoreSettings = callingClass; // Note the use of self! 
    isLivescoresSettingsView = YES; 
    addEventToList = YES; 
} 
+0

我完全同意但是:D ---只是爲了添加信息:你不需要變量或@synthesize了 - 只需一個屬性就足夠了(Xcode 4.5+) –

+1

@ Daij-Djan同意; Xcode/clang的新特性並不是必需的。也許我是老式的,因爲我喜歡查看類定義,並查看它使用的實例變量,而不必從查看「@屬性」部分查看它。對我來說這不是Apple的改進;只是一個懶惰的藉口... – trojanfoe

+0

其實我試過這個..使它成爲一個屬性..但是不起作用...當第二個方法被調用時它失去了它的參考.. –