尝试取消归档多个对象时,NSKeyedUnarchiver 会卡住
我们有一个自定义矩阵类,并且我们正在尝试归档和取消归档包含四个矩阵类的 NSArray。第一个似乎很好地解档(我们可以看到 initWithCoder 被调用一次),但随后程序只是挂起,使用 100% CPU。它不会继续或输出任何错误。这些是矩阵类的相关方法(行、列和矩阵是我们唯一的实例变量):
-(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"];
我们尝试过一次归档一个文件(没有 NSArray),每个文件都归档到不同的文件,但它仍然挂在第二个文件上。
让我们疯狂的是我们可以很好地归档和取消归档单个矩阵。我们已经(成功地)使用了比这四个组合大很多倍的矩阵来做到这一点。
We've got a custom matrix class, and we're attempting to archive and unarchive an NSArray containing four of them. The first seems to get unarchived fine (we can see that initWithCoder is called once), but then the program simply hangs, using 100% CPU. It doesn't continue or output any errors. These are the relevant methods from the matrix class (rows, columns, and matrix are our only instance variables):
-(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;
}
And this is really all we're trying to do:
NSArray * weightMatrices = [NSArray arrayWithObjects:w1,w2,w3,w4,nil];
[NSKeyedArchiver archiveRootObject:weightMatrices toFile:@"weights.archive"];
NSArray * newWeights = [NSKeyedUnarchiver unarchiveObjectWithFile:@"weights.archive"];
We've tried archiving one at a time (no NSArray), each to a different file, but it still hangs on the second one.
What's driving us crazy is that we can archive and unarchive a single matrix just fine. We've done so (successfully) with a matrix many times larger than these four combined.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在你的 initWithCoder 方法中:
本质上你正在覆盖你的堆栈,这将导致未定义的结果。
应该是:
In your initWithCoder method:
IN essence you are overwriting your stack, which will cause undefined results.
Should be: