-1

メソッドで View オブジェクトが突然 nil になりました。

私はARCを使用していません

スレッド化は関係ありません


何が起こっているのかというと、最初にその1stmethodメソッドを呼び出すと、すべてが正常に機能し、livescoreSettings への参照が保持されます。

次に2ndmethodメソッドを呼び出すと、livescoreSettings ref も保持されますが、delegate メソッドがアクティブになるまでに、その変数の参照が失われます..理由がわからない...

@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に渡される所有権を取得していないことです。ARC を使用していない場合は、それらのメソッドと独自のメソッドでARC を使用する必要があります(単純にインスタンスを に割り当てるだけでは、MRR を使用する場合の保持カウントは増加しません)。1stmethod2ndmethodretainreleasedealloclivescoreSetting

1stmethodが次のように呼び出された場合を想像してください。

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

その後、(1)viewに割り当てられてwhateverItsCalled.livescoreSettingいますが、保持カウントは 1 です。(2) の後、保持カウントは 0 ですwhateverItsCalled.livescoreSettingが、現在はダングリング ポインターであり、「メッセージが割り当て解除に送信されました」などのメッセージが表示されないことに驚いています。表示されているエラーではなく、オブジェクト" ( nilARC が関与していないときに割り当てられている理由がわかりません)。

この問題を解決するにはsynthesize、インスタンス変数の setter/getter メソッドに を追加する必要があります@property_セッター/ゲッター メソッドの名前と区別するために、先頭にアンダースコア ( ) を使用してインスタンス変数に名前を付けることを好みます。それで:

.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 に答える