1

The problem:

I want to create a temporary file that holds RAW data from the asset I've read through ALAssetLibrary. But as soon as I try to create a file via NSFileManager´s [createFileAtPath:contents:attributes:] the application stops and throws a EXC_BAD_ACCESS. Well, that error often means that I have a broken or faulty pointer but there is really nothing in my code that is set to nil except for the fileHandle IVAR.

I managed to run the code a couple of times successfully but after that it crashes every single time it comes to create the file under iOS7+. iOS versions below that seems to be able to create and write to the file, but as soon as I try to access it in any way, the same error comes up.

  • Just to clarify: There are NO files present in the "tmp" directory.
  • Below, the full path to the temp file is: /private/var/mobile/Applications/83BFD709-3587-404D-97E7-BEAAA2860283/tmp/temp.tmp

The error prone code:

 - (NSString *)applicationTempDirectory
{

   // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    //NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;

    NSString* basePath = NSTemporaryDirectory();
    return basePath;
}

Below follows the part that cause the crash.

.... <NEW METHOD>

NSUInteger chunkSize = 1000 * 1024; //1MB chunks
ALAssetRepresentation* rep = [asset_ defaultRepresentation];
uint8_t chunkbuffer[chunkSize];
long long length = [rep size];

// Copy file
NSString* tempFile = [NSString stringWithFormat:@"%@/temp.tmp", [self applicationTempDirectory]];
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSFileHandle* fileHandle = [NSFileHandle fileHandleForWritingAtPath: tempFile];

if(!fileHandle) {
    NSLog(@"File path_: %@", tempFile);
    [fileMgr createFileAtPath:tempFile contents:nil attributes:nil]; //<--- Here it throws the exception
    fileHandle = [NSFileHandle fileHandleForWritingAtPath:tempFile];
}

NSUInteger offset = 0;
do {
    NSUInteger bytesCopied = [rep getBytes:chunkbuffer fromOffset:offset length:sizeof(chunkbuffer) error:nil];
    offset += bytesCopied;
    NSData *data = [[NSData alloc] initWithBytes:chunkbuffer length:bytesCopied];
    [fileHandle writeData:data];
    data=nil;
} while (offset < length);

[fileHandle closeFile];
fileHandle = nil;

.... <CONTINUES ON>

The question:

Does anyone have any clue to why this seemingly easy task is not working as expected? I have tried literally everything except to set any attributes to the [createFileAtPath:contents:attributes:]. I've searched the web for everything, but no one seems to have the same problem as i do. Some here on SO are close, but after trying the supplied solutions, it didn't work...

svrushal
  • 1,612
  • 14
  • 25
Widerberg
  • 1,118
  • 1
  • 10
  • 24
  • Why dont you simply copy the file using NSFileManager ? – Sandeep May 13 '14 at 09:46
  • What happens if you put some dummy data in (temporarily) to the "`contents:`" parameter? [See the answer in this related question](http://stackoverflow.com/a/1881008/981049) to see what I am talking about. I wonder if the "`nil`" being passed to "`contents:`" is the problem. – Michael Dautermann May 13 '14 at 09:48
  • @insane-36 is that even possible due to the sandboxing from camera roll? – Widerberg May 13 '14 at 09:50
  • @MichaelDautermann I've already tried that without success :/ – Widerberg May 13 '14 at 09:50
  • Where exactly does the crash happen? Run with debugger, at the crash check which code of yours called the crashing code. Did you actually step through your code, step by step, and checked everything? – gnasher729 May 13 '14 at 09:55
  • @gnasher729 the line the program crashes on is clearly indicated. – dandan78 May 13 '14 at 09:57
  • Yes, that was the first thing i did. All is OK. The program runs fine. But as soon as i try to create the file, it doesn't work and cause a crash. – Widerberg May 13 '14 at 09:57

1 Answers1

0
[fileMgr createFileAtPath:tempFile contents:nil attributes:nil];

You are creating a file without any data in it. According to the documentation, although the attributes parameter can be nil, nothing is specified about contents. It is likely that this is the cause of your problem.

dandan78
  • 13,328
  • 13
  • 64
  • 78
  • Sadly this is not the case here. I tried to put dummy data into the file, but to no avail... Still EXC_BAD_ACCESS. – Widerberg May 13 '14 at 09:56