我的方法中有一个View对象突然变为零。
我没有使用ARC
不涉及线程
发生的事情是,我第一次称 1stmethod 方法一切正常,并且保留了对livescoreSettings的引用。
接下来当我调用 2ndmethod 方法时,livescoreSettings 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
答案 0 :(得分:1)
问题是,您没有将livescoreSetting
的所有权转移到1stmethod
或2ndmethod
。如果您不使用ARC,那么您需要retain
使用这些方法,release
使用dealloc
方法(只需将实例分配给livescoreSetting
不使用MRR时增加保留计数。)
想象一下,如果以这种方式调用1stmethod
:
LivescoreSettingsView *view = [[LivescoreSettingsView alloc] init];
[whateverItsCalled 1stmethod:view; // (1)
[view release]; // (2)
然后在{1}处将view
分配给whateverItsCalled.livescoreSetting
,但保留计数为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;
}