1
votes

Does anyone know why there would be one-off errors with NSLog and NSString? It works fine in 99% of my program, but for some reason this error appears in one of my model's description method:

Example code:
localFileId = 7;
type = 2;
localId = 5;
NSLog(@"CachedFile localId=%d, 2=%d, localFileId=%d, type=%d, path=%@", localId, 2, localFileId, type, self.path);

Example Result: 
CachedFile localId=5, 2=0, localFileId=2, type=7, path=(null)

Notice the "0" that gets inserted in there, where it should be "2=2". This happens with NSString stringWithFormat as well.

1

1 Answers

0
votes

%d expects an int or long parameter, that's 4 bytes on the stack.

You don't show the declaration for localId, is it the right size? If it is 2 bytes when %d tells NSLog to read 4 bytes, that would mean NSLog reads the first 2 bytes of the next parameter too, and then that one is off by 2 bytes, etc.

EDIT: OK, that's wrong, I just tried it. I actually have it backwards, instead of localId being too short (< 4 bytes), it's too long (> 4 bytes), see @dreamlax's comments below.

short shortX = 1;
int intX = 2;
NSLog(@"shortX = %d, intX = %d", shortX, intX);

prints: shortX = 1, intX = 2