I have a category method that does somethign like this:
@implementation NSData (additions)
- (id)someFunc {
char *buffer = malloc(255);
NSUInteger length = 0;
while (true) {
// . . . fill buffer[length++];
}
realloc(buffer, length);
return [NSData dataWithBytesNoCopy:buffer length:length freeWhenDone:NO];
}
I am then trying to write the returned data (well call this NSData *fileData):
NSError *error;
NSData fileData = [NSData someFunc];
[fileData writeToFile:somePath options:NSDataWritingAtomic error:&error];
I get an error:
Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0x20deffd0 {NSFilePath=/Users/user/Library/Application Support/iPhone Simulator/4.3/Applications/4C315580-153D-4FA7-9474-E17B58832515/Library/Caches/file.pdf, NSUnderlyingError=0x20de1fe0 "The operation couldn’t be completed. Bad address"}
The path exists and is valid. I think the issue is the returned NSData is just a light wrapper around an array allocated with malloc and that writeToFile does not know how to handle this. Does anyone see what I am doing wrong?
buffer = realloc(buffer, length);
– DarkDust