カスタム マトリックス クラスがあり、そのうちの 4 つを含む NSArray をアーカイブおよびアーカイブ解除しようとしています。最初のものは正常に解凍されたように見えますが (initWithCoder が 1 回呼び出されていることがわかります)、その後、プログラムは 100% の CPU を使用して単にハングします。続行しないか、エラーを出力しません。これらは、matrix クラスの関連するメソッドです (rows、columns、matrix は唯一のインスタンス変数です)。
-(void)encodeWithCoder:(NSCoder*) coder {
float temp[rows * columns];
for(int i = 0; i < rows; i++) {
for(int j = 0; j < columns; j++) {
temp[columns * i + j] = matrix[i][j];
}
}
[coder encodeBytes:(const void *)temp length:rows*columns*sizeof(float) forKey:@"matrix"];
[coder encodeInteger:rows forKey:@"rows"];
[coder encodeInteger:columns forKey:@"columns"];
}
-(id)initWithCoder:(NSCoder *) coder {
if (self = [super init]) {
rows = [coder decodeIntegerForKey:@"rows"];
columns = [coder decodeIntegerForKey:@"columns"];
NSUInteger * len;
*len = (unsigned int)(rows * columns * sizeof(float));
float * temp = (float * )[coder decodeBytesForKey:@"matrix" returnedLength:len];
matrix = (float ** )calloc(rows, sizeof(float*));
for (int i = 0; i < rows; i++) {
matrix[i] = (float*)calloc(columns, sizeof(float));
}
for(int i = 0; i < rows *columns; i++) {
matrix[i / columns][i % columns] = temp[i];
}
}
return self;
}
そして、これが実際に私たちがやろうとしているすべてです:
NSArray * weightMatrices = [NSArray arrayWithObjects:w1,w2,w3,w4,nil];
[NSKeyedArchiver archiveRootObject:weightMatrices toFile:@"weights.archive"];
NSArray * newWeights = [NSKeyedUnarchiver unarchiveObjectWithFile:@"weights.archive"];
一度に 1 つずつ (NSArray ではなく)、それぞれ別のファイルにアーカイブしようとしましたが、まだ 2 番目のファイルにハングアップしています。
私たちを夢中にさせているのは、1 つのマトリックスを問題なくアーカイブおよびアンアーカイブできることです。これら 4 つを合わせた数倍の行列を使用して (成功裏に) 実行しました。