尝试取消归档多个对象时,NSKeyedUnarchiver 会卡住

发布于 2024-08-31 11:52:05 字数 1800 浏览 10 评论 0原文

我们有一个自定义矩阵类,并且我们正在尝试归档和取消归档包含四个矩阵类的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舞袖。长 2024-09-07 11:52:05

在你的 initWithCoder 方法中:

NSUInteger * len;
*len = (unsigned int)(rows * columns * sizeof(float));

本质上你正在覆盖你的堆栈,这将导致未定义的结果。

应该是:

NSUInteger len = (unsigned int)(rows * columns * sizeof(float));
float * temp = (float * )[coder decodeBytesForKey:@"matrix" returnedLength:&len];

In your initWithCoder method:

NSUInteger * len;
*len = (unsigned int)(rows * columns * sizeof(float));

IN essence you are overwriting your stack, which will cause undefined results.

Should be:

NSUInteger len = (unsigned int)(rows * columns * sizeof(float));
float * temp = (float * )[coder decodeBytesForKey:@"matrix" returnedLength:&len];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文