【问题标题】:View object becoming nil abruptly - xcode查看对象突然变为 nil - xcode
【发布时间】:2013-01-23 06:49:38
【问题描述】:

我的方法中有一个 View 对象突然变为 nil。

我没有使用 ARC

不涉及线程


发生的事情是,我第一次调用 1stmethod 方法时一切正常,并且保留了对 livecoreSettings 的引用。

接下来,当我调用 2ndmethod 方法时,还保留了 livecoreSettings 引用,但是当委托方法被激活时,该变量的引用丢失了..不知道为什么...

@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

【问题讨论】:

  • livescoreSettings 也是一个实例变量吗?
  • No ARC...livescoreSettings 不是实例变量。它在 .h 文件中声明
  • no.. 不涉及线程
  • 好的,如果lovescoreSettings 不是实例变量,那么它在哪里定义?这就像 20 个问题,不应该是这样!
  • 如果您在我的代码中看到注释,我已经提到了与.h 文件中声明的相同@interface

标签: xcode reference ios6 retaincount


【解决方案1】:

问题是您没有获得livescoreSetting 的所有权,该所有权被传递到1stmethod2ndmethod。如果您没有使用 ARC,那么您将需要在这些方法中 retain 它并在您的 dealloc 方法中 release 它(只需将实例分配给 livescoreSetting 不会增加保留使用 MRR 时计数)。

想象一下,如果1stmethod 以这种方式被调用:

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

然后view 在 (1) 处被分配给 whateverItsCalled.livescoreSetting,但保留计数为 1。在 (2) 之后保留计数为 0,但 whateverItsCalled.livescoreSetting 现在是一个悬空指针,我很惊讶您看不到诸如“发送到已释放对象的消息”之类的消息,而不是您看到的错误(我不明白为什么在不涉及 ARC 时将其分配给 nil)。

要解决此问题,您需要通过为实例变量添加@property 来为实例变量添加synthesize 您的setter/getter 方法。我更喜欢使用前导下划线 (_) 来命名实例变量,以将它们与 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;
}

【讨论】:

  • 我完全同意,但是:D --- 只是为了添加信息:你不再需要变量或 @synthesize 了——只要一个属性就足够了(Xcode 4.5+)
  • @Daij-Djan 同意;使用 Xcode/clang 的新功能,这不是必需的。也许我过时了,因为我喜欢查看类定义并查​​看它使用的实例变量,而不是通过查看@property 部分来解决它。对我来说,这不是苹果的改进。只是懒惰的借口......
  • 实际上我试过这个..使它成为一个属性..但不起作用......当调用 secondMethod 时它失去了它的引用..
  • @trojanfoe - 谢谢。您的回答帮助解决了问题
猜你喜欢
  • 2012-04-27
  • 1970-01-01
  • 2011-04-25
  • 1970-01-01
  • 2022-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多