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

Nutter

macrumors 6502
Original poster
Mar 31, 2005
432
0
London, England
Anyone have any idea why this code causes a SIGBUS 10 error?

Code:
@inferface NSString (TempFiles)
+(NSString *) temporaryFileName;
@end

@implementation NSString (TempFiles)
+(NSString *) temporaryFileName
{
	NSProcessInfo *proc = [NSProcessInfo processInfo];
	NSString *tempdir = NSTemporaryDirectory();
	
	return [tempdir stringByAppendingPathComponent: [proc globallyUniqueString]];
}
@end

Surely I should be able to add a category to NSString? My Objective-C book instructs me to do so!
 

rinseout

macrumors regular
Jan 20, 2004
160
0
Nutter said:
Anyone have any idea why this code causes a SIGBUS 10 error?
Could it be a memory thing? Does stringByAppendingPathComponent return something in the autorelease pool, in which case you'll want to retain it? I'm just learning Cocoa/ObjC myself, so this is really just a shot in the dark.
 

Nutter

macrumors 6502
Original poster
Mar 31, 2005
432
0
London, England
Well, the string is in the auto-release pool, but I don't need to retain it unless I need to use it outside the scope of the current pool.

Anyway, that shouldn't cause a crash!

Thanks for the suggestion anyway. I'm still at a loss.
 

rinseout

macrumors regular
Jan 20, 2004
160
0
Nutter said:
Well, the string is in the auto-release pool, but I don't need to retain it unless I need to use it outside the scope of the current pool.

Anyway, that shouldn't cause a crash!

Thanks for the suggestion anyway. I'm still at a loss.
OK. I admit I don't fully understand the autorelease rules; I thought that it might be autoreleased when you go out of scope at the end of the temporaryFileName method, so that the pointer isn't valid when you try to use it (which would cause a crash). What line is it crashing on?
 

Nutter

macrumors 6502
Original poster
Mar 31, 2005
432
0
London, England
No, that would defeat the whole object of the auto-release pool! All the objects remain intact until you release the pool itself, at which point the objects in the pool get released as well (unless they have been retained).

The trick is that most of the time you will release the pool and start a new one every time the program comes back to the event loop, so the pool is emptied quite frequently.

As far as I can tell, it crashes before it even executes a single line!
 

rinseout

macrumors regular
Jan 20, 2004
160
0
OK. It sounds like you know what you're doing, so I can't help (since I don't ! :D) Do you need to make sure the category interface is #imported in the file where you're calling the new method? (I expect not, because it's all bound at runtime anyway).
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,629
Western US
I don't think there is anything wrong with your code. I compiled it and called the method with the code:
Code:
NSString *blob = [NSString temporaryFileName];
NSLog(@"Temp file name = %@", blob);
and here's the output I got:
Code:
2005-08-12 22:21:02.706 StringExtensionTest[14032] Temp file name = /private/var/tmp/folders.501/TemporaryItems/E19D6650-C4B6-4BAE-8365-989B442523A5-14032-00000BE370846791
It does not crash, so I can only assume it's something weird you're doing with the string that is returned or something else in your code entirely. You'll have to narrow down where exactly it's crashing.
 

Nutter

macrumors 6502
Original poster
Mar 31, 2005
432
0
London, England
Thanks for the reply.

Here's my code:
Code:
int main (int argc, const char * argv[]) 
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	NSString *theString;
	
	theString = [NSString temporaryFileName];
	printf("%s\n", [theString cString]);
	
    [pool release];
    return 0;
}

I've tried putting a break point at the first line of main() and running in debug mode, but before it reaches the break point it gives the message 'GDB: Program received signal: "EXC_BAD_ACCESS".'

I'm tearing my hair out...
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,629
Western US
Wow, I really don't know, this time I included your file exactly and got:
Code:
/private/var/tmp/folders.501/TemporaryItems/779BD8AC-E915-4639-AC42-6E494E4AF537-14377-00000CB52D596EC0
and again, no crash. So I think something in the setup or build settings must be messed up. Now I created this project using the Foundation Tool type, is that what you used? You will have to have Foundation available to use NSStrings anyway. So I really don't know. I would say try to start a new project from scratch and copy your code over, maybe something got corrupted somewhere. It doesn't really make sense for it to be crashing before it even gets to main().
 

Nutter

macrumors 6502
Original poster
Mar 31, 2005
432
0
London, England
Thanks.

I worked it out in the end -- very stupidly, I had mispelled "@interface" as "@inferface"...!

Easy to fix, but I hadn't realised that such a basic error would be passed over by the compiler. It seems that all objective-C compiler directives are sensitive to this kind of error.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.