Help with attributesOfItemAtPath

Discussion in 'Mac Programming' started by EggWhite, Jan 19, 2010.

  1. EggWhite macrumors member

    Joined:
    Aug 28, 2009
    #1
    I got a function online that takes and NSString of a path and returns the inode value. But when I pass it a string it always errors with the following
    Program received signal: “EXC_BAD_ACCESS”.

    Here are the two strings I have tried, do I need to put then in some sort of special format.
    NSString * temp = @"/Users/username/Desktop/Dropbox/Deleted/KeePass/KeePass.exe";
    NSString *temp =@"/System/Library/Fonts/AppleGothic.ttf";

    PHP:
    long getInode(NSStringpath) {
        
    NSFileManagerfm = [NSFileManager defaultManager];
        
    NSErrorerror;
        
    NSDictionaryinfo = [fm attributesOfItemAtPath:path error:&error];
        
    NSNumberinode = [info objectForKey:NSFileSystemFileNumber];
        if (
    error) {
            [
    NSApp presentError:error];
        }    
        return [
    inode longValue];
    }
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
  3. EggWhite thread starter macrumors member

    Joined:
    Aug 28, 2009
    #3
    Sorry should have been more clear. The code doesn't die, but the following prints "Program received signal: “EXC_BAD_ACCESS”." to the console


    if (error) {
    [NSApp presentError:error];
    }
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    I don't think that's the result you'd get from that line. That looks like a signal from the system. If you're running in XCode you get a backtrace by entering bt at the gdb prompt when execution stops.

    -Lee
     
  5. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #5
    I would guess that what is happening is the NSNumber is not being returned because the dictionary may not have that key. Add a check to see if you have a non-nil value for inode.
     
  6. EggWhite thread starter macrumors member

    Joined:
    Aug 28, 2009
    #6
    I stepped through the code line by line and it does seem to die now on "[NSApp presentError:error]; ", which I added when I was debuging to see why I wasn't getting the correct result.

    I think my issue is because I didn't initialize *error to nil then it was always entering the if(error) check and I am getting the bad access error since that pointer value is no good. Does that make sense?

    Sorry for the dumb questions this is my first OSX app, and I only started with iPhone programing less then a year ago so I am still learning.
     
  7. kpua macrumors 6502

    Joined:
    Jul 25, 2006
    #7
    Your suspicion about not initializing the error pointer is correct. It's a random value, so unless you get lucky, the 'if (error)' branch is taken, and a bogus pointer is passed to -[NSApp presentError:], causing a crash when it is later dereferenced.
     
  8. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #8
    Read the documentation of attributesOfItemAtPath. error is _only_ set if an error occurs. So what does error contain if there was no error?
     
  9. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #9
    The sound of a tree falling when no one is around to hear it?

    -Lee
     
  10. kpua macrumors 6502

    Joined:
    Jul 25, 2006
    #10
    It contains whatever it contained before the call. In this case, it contains random unpredictable garbage, whatever was left on the stack when the method was called.

    (Nice one lee :D)
     
  11. EggWhite thread starter macrumors member

    Joined:
    Aug 28, 2009
    #11
    Thanks for all the help with this issue. Everything seems to be working fine now.
     

Share This Page