私は iPad アプリを開発していますが、ここで非常に奇妙な問題に遭遇しました。私はそれをできるだけうまく説明しようとします。
TranslationObject
キーとテキスト値を保持しているという名前のクラスがあります。このクラスを次のように作成しました。
@interface TranslationObject : NSObject {
NSNumber *_key;
NSString *_value;
}
@property (nonatomic, retain) NSNumber *key;
@property (nonatomic, retain) NSString *value;
- (id) initWithKey:(NSNumber *) key andValue:(NSString *) value;
@end
翻訳は将来的に XML または DB から取得される予定ですが、現時点では次のようにしています。
@interface Translation : NSObject {
NSMutableArray *m_extfeat;
}
@property (nonatomic, retain) NSMutableArray *extfeat;
+ (Translation *) getInstance;
- (id) init;
- (NSMutableArray *) getExtFeat;
@end
実装:
@implementation Translation
@synthesize extfeat = m_extfeat;
- (id) init {
self = [super init];
if (self) {
m_extfeat = [[self getExtFeat] retain];
}
return self;
}
- (NSMutableArray *) getExtFeat {
TranslationObject *obj1 = [[[TranslationObject alloc] initWithKey:[NSNumber numberWithInt: 0] andValue:@"Animal house"] autorelease];
.... more items declared ....
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:obj1, obj2, obj3, obj4, obj5, obj6, obj7, obj8, obj9, obj10, obj11, obj12, obj13, obj14, obj15, obj16, obj17, nil];
return [array autorelease];
}
@end
これらの翻訳は で使用されており、メソッドで次のようUITableViewController
にフェッチされています。viewDidLoad
- (void)viewDidLoad
{
_data = [[Translation getInstance].extfeat retain];
}
これらの値をその で使用しcellForRowAtIndexPath
、そこでメソッドを呼び出してセルを構成します。
- (void) configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *) indexPath {
TranslationObject *object = (TranslationObject *) [_data objectAtIndex:indexPath.row];
//Crashes here at 13th item:
NSLog("Object key: %@", [object.key stringValue]);
}
上記のスニペットが示すように、奇妙なことに、_data 配列に 12 を超えるアイテムが含まれている場合にのみ、キー値をフェッチするときにアプリがクラッシュします。したがって、_data 変数に 12 個以下のアイテムのみを入力すると、コードは正常に動作します。12 個以上追加すると、13 番目のオブジェクトをフェッチするとすぐにアプリがクラッシュします。
NSZombies を有効にしたので、そのメソッドの 13 番目の項目を確認すると、値は問題ありませんが、ゾンビになったのはキーだけです。そしてまた.. 13番目のアイテムからのみ!
これがどのように可能か知っている人はいますか?メモリに保存できるアイテムの最大数があるということでしょうか?12項目でメモリーフルですか?しかし、その場合、なぜ値がまだそこにあるのでしょうか。それが以前にリリースされたキーだけである可能性はありますか?! そしてどうやって?!
この説明が理にかなっていて、誰かがこの事件に光を当てることができることを願っています.. =/
ありがとう!
編集: TranslationObject の initWithKey メソッドの実装は次のとおりです。
- (id) initWithKey:(NSNumber *) key andValue:(NSString *) value {
self = [super init];
if (self) {
_key = key;
_value = value;
}
自分自身を返します。}