user1072543
user1072543

Reputation:

Bad access calling Objective-C method

I am new to objective-c. I've taken classes in both python and c++, but am really interested in objective-c so I've been working on learning on my own the last couple of days. This program is supposed to compare files in two locations and add missing ones. A simple sync, although not all features are currently implemented.

The error arrises from the recursivelyGetContentsOfDirectory function. It is returning the error EXEC_BAD_ACCESS. Since it's not actually in the function I'm assuming that it's something to do with how it's defined.

   #import <AppKit/AppKit.h>

@interface Sync : NSControl{
    NSURL *baseToDirectory;
    NSURL *baseFromDirectory;

    NSArray *filesInBaseFromDirectory;

    IBOutlet NSTextField *baseToDirectoryLabel;
    IBOutlet NSTextField *baseFromDirectoryLabel;
}
- (IBAction)chooseBaseSyncFromDirectory:(id)sender;
- (IBAction)chooseBaseSyncToDirectory:(id)sender;
- (IBAction)performSync:(id)sender;

void recursivelyGetContentsOfDirectory (NSURL *path);

@end

implementation file

#import "Sync.h"

@implementation Sync


- (IBAction)chooseBaseSyncFromDirectory:(id)sender {

    NSOpenPanel* openDlg = [NSOpenPanel openPanel];
    [openDlg setCanChooseDirectories:YES];
    [openDlg setCanChooseFiles:NO];
    //Open the directory
    if ([openDlg runModal] == NSFileHandlingPanelOKButton) {
        NSArray *files = [openDlg URLs];
        NSString *fileAsString = [[files objectAtIndex:0] path];
        [baseFromDirectoryLabel setStringValue:fileAsString];
        baseFromDirectory = [files objectAtIndex:0];
        recursivelyGetContentsOfDirectory(baseFromDirectory);
    }
}

- (IBAction)chooseBaseSyncToDirectory:(id)sender {
    NSOpenPanel* openDlg = [NSOpenPanel openPanel];
    [openDlg setCanChooseDirectories:YES];
    [openDlg setCanChooseFiles:NO];

    if ([openDlg runModal] == NSFileHandlingPanelOKButton) {
        NSArray *files = [openDlg URLs];
        NSString *fileAsString = [[files objectAtIndex:0] path];
        [baseToDirectoryLabel setStringValue:fileAsString];
        baseToDirectory = [files objectAtIndex:0];
    }
}

void recursivelyGetContentsOfDirectory (NSURL *path){
    NSFileManager *tempManager;
    NSArray *contentsOfDirectory = [tempManager contentsOfDirectoryAtPath:[path path] error:nil];
    for (int i = 0; i < (int)[contentsOfDirectory count] ; i = i+1) {
        if ([[contentsOfDirectory objectAtIndex:i] isDirectory]) {
            NSString *next = [NSString stringWithString:[path path]];
            [next stringByAppendingString:[contentsOfDirectory objectAtIndex:i]];
            NSURL *nextPath = [NSURL URLWithString:next];
            recursivelyGetContentsOfDirectory(nextPath);
        } else {
            NSLog(@"Non-Directory Found");
        }
    }
}

- (IBAction)performSync:(id)sender {

}

@end

Upvotes: 2

Views: 430

Answers (1)

vikingosegundo
vikingosegundo

Reputation: 52227

NSFileManager *tempManager;

You are not creating the tempManager

it should be something like

NSFileManager *tempManager=[[NSFileManager alloc] init];

if you are not using ARC, you are responsible to (auto)release tempManager.

or use NSFileManager *tempManager = [NSFileManager defaultManager]

Upvotes: 2

Related Questions