NSKeyedUnarchiver chokes when trying to unarchive more than one object

Posted by ajduff574 on Stack Overflow See other posts from Stack Overflow or by ajduff574
Published on 2010-05-02T07:16:19Z Indexed on 2010/05/02 7:17 UTC
Read the original article Hit count: 335

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"];

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.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about ipod