1

私のiPhoneアプリケーションは、UITableViewデリゲートメソッドの1つからインスタンス変数にアクセスすると爆発します。保持していると思うので、なぜ問題なくアクセスできないのかわかりません。

これが私の.hファイルです

#import <Foundation/Foundation.h>
#import "AlertSummaryCell.h"
#import "AlertDetailViewController.h"

@interface AlertSummaryTableViewController : UITableViewController {
NSDictionary *alerts;
NSString *alertKind;
}

@property(nonatomic、retain)NSDictionary * alert; @property(非アトミック、保持)NSString * alertKind;

@終わり

私の.mでは、アプリケーションは最初のNSLog呼び出しで停止します。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"AlertSummaryTableViewController.numberOfRowsInSection entered");
NSLog(@"  alerts description = %@", [alerts description]);
// To know how many alert summaries we have, get the value for count out of the input dictionary
int theCount = [[alerts objectForKey:@"count"] intValue];
NSLog(@"  Going to return %d",theCount);
return theCount;
}

私は何が欠けていますか?

viewDidLoadメソッドにはまったく問題はありません。

 - (void)viewDidLoad {
 NSLog(@"AlertSummaryTableViewController.viewDidLoad entered");
 NSLog(@"  alerts description = %@", [alerts description]);

 // We want the View title to include the alert count
 // To know how many alert summaries we have, get the value for count out of the input dictionary
 int theCount = [[alerts objectForKey:@"count"] intValue];

 // Now construct the title text and set our views title to it
 NSString *myTitle = [[NSString alloc] initWithFormat:@"%@ Alerts (%d)",alertKind,theCount];
 [self setTitle: myTitle];

 // Memory cleanup
 [myTitle release];

 [super viewDidLoad];
 }
4

3 に答える 3

4

EXC_BAD_ACCESS エラーの場合、通常、解放されたオブジェクトにメッセージを送信しようとしています。これらを追跡する最良の方法は、NSZombieEnabled使用することです。

これは、オブジェクトを実際に解放することはありませんが、オブジェクトを「ゾンビ」としてラップし、通常は解放されることを示すフラグを内部に設定することによって機能します。このようにして、もう一度アクセスしようとしても、エラーが発生する前に何があったかがわかります。この少しの情報を使用して、通常は後戻りして問題が何であったかを確認できます。

これは、デバッガーが有用な情報をときどき出すバックグラウンド スレッドで特に役立ちます。

ただし、注意すべき非常に重要なことは、これが配布コードではなくデバッグ コードにのみ含まれていることを 100% 確認する必要があるということです。何もリリースされないため、アプリはリーク、リーク、リークします。これを行うことを思い出させるために、このログを appdelegate に入れました。

if(getenv("NSZombieEnabled") || getenv("NSAutoreleaseFreedObjectCheckEnabled"))
  NSLog(@"NSZombieEnabled/NSAutoreleaseFreedObjectCheckEnabled enabled!");
于 2009-09-20T04:52:34.323 に答える
0

アラートが設定されていることを確認したと思います。nil 参照で [alerts objectForKey:] を呼び出そうとすると、そのエラーが発生します。

于 2009-09-21T02:16:42.150 に答える
0

問題は、アラートの設定方法にあることが判明しました。オブジェクトを保持するセッターメソッドを使用するのではなく、私は無意識のうちにこれを行いました:

alerts = [[UIDictionary alloc] init];     // or something like this

私が推測するように、これは警告やエラーなしでコンパイルされました。

セッターを使用することで、

[self setAlerts:[[UIDictionary alloc] init];   // again I'm going from memory so ...

私が作成しなければならなかったアラートは、必要に応じて「保持」され、View Controllerがロードされるとすべてがうまくいきました。

私はObjective-Cに非常に慣れておらず、メモリ管理をいじる必要がありますが、それとiPhone SDKを約4週間使用した後、ようやく光が見え始めています.

于 2009-09-22T00:21:36.573 に答える