Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

EggWhite

macrumors member
Original poster
Aug 28, 2009
40
0
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(NSString* path) {
	NSFileManager* fm = [NSFileManager defaultManager];
	NSError* error;
	NSDictionary* info = [fm attributesOfItemAtPath:path error:&error];
	NSNumber* inode = [info objectForKey:NSFileSystemFileNumber];
	if (error) {
		[NSApp presentError:error];
	}	
	return [inode longValue];
}
 
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];
}
 
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
 
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.
 
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.
 
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.
 
Read the documentation of attributesOfItemAtPath. error is _only_ set if an error occurs. So what does error contain if there was no error?
 
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)
 
Thanks for all the help with this issue. Everything seems to be working fine now.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.