Mert Dumenci
Mert Dumenci

Reputation: 91

EXC_BAD_ACCESS on stringByAppendingPathComponent

I want my application to copy the video you taken with it to a directory, inside the App Sandbox.

Here is what I've wrote so far.

- (void) imagePickerController:(UIImagePickerController *)picker 
 didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
    int r = arc4random() % 100;
    int s = arc4random() % 100;
    int e = arc4random() % 100;
    NSLog(@"%i%i%i.MOV", r, s, e);
    NSString *tempFileName = (@"%i%i%i.MOV", r, s, e);
    NSString *tempFilePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
    NSString *documentsDirectory 
        = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                               NSUserDomainMask, YES)
                                 objectAtIndex:0];
    NSString *storePath = [documentsDirectory stringByAppendingPathComponent:tempFileName];

    NSError *error = nil;
    [[NSFileManager defaultManager] copyItemAtURL:tempFilePath
                                            toURL:[NSURL fileURLWithPath:storePath]
                                            error:&error];

But after I test my app on my iPhone, program gets stuck after pressing use after taking video. xCode tells EXC_BAD_ACCESS on

NSString * storePath = [documentsDirectory stringByAppendingPathComponent:tempFileName];

What can be wrong here?

And uh, I actually want to 'create' a new directory called videos inside the app sandbox and save in it. I couldn't do that, so I am using documents directory.

Thanks!

Upvotes: 1

Views: 405

Answers (3)

chown
chown

Reputation: 52748

You are sending the message

isFileURL

to a NSString somehow and it is not a recognized message for NSString.

Turn on malloc stack logging, guard malloc, and zombie enabled in the debugger, then run:

(gdb) info malloc-history 0x1b18b0

Where 0x1b18b0 is the address of the thing being sent the unrecognized selector. It should give you more info about where in your code the problem is.

Upvotes: 1

Joe
Joe

Reputation: 57179

The problem lies with this line

NSString * tempFileName = (@"%i%i%i.MOV", r, s, e);

Instead of actually formatting a string you are using the comma operator which will ultimately sets tempFileName to the random number stored in e which will be an invalid address. That line is equivalent to the following.

@"%i%i%i.MOV";
r;
s;
//Now here is where the pointer is set to an invalid address
//resulting in EXC_BAD_ACCESS
NSString * tempFileName = e;

To fix this all you have to do is actually format the string. And to fix another issue you need to actually make a URL for tempFilePath like you do for storePath.

NSString * tempFileName = [NSString stringWithFormat:@"%i%i%i.MOV", r, s, e];

...

NSError *error = nil;
[[NSFileManager defaultManager] copyItemAtURL:[NSURL fileURLWithPath:tempFilePath]
                                        toURL:[NSURL fileURLWithPath:storePath]
                                        error:&error];

Upvotes: 1

progrmr
progrmr

Reputation: 77211

The exception trace shows that your trying to send the isFileURL: method to an NSString which is not a valid method on an NSString. I don't see any isFileURL: in the code you posted, check your code to see where you might be doing that.

Upvotes: 1

Related Questions