2013-01-23 28 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

其实我试过这个..使它成为一个属性..但是不起作用...当第二个方法被调用时它失去了它的参考.. –