-1

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

我没有使用 ARC

不涉及线程


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

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

@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
4

1 回答 1

1

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

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

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

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

要解决此问题,您需要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;
}
于 2013-01-25T12:59:04.447 回答