Categories problem

Discussion in 'Mac Programming' started by Nutter, Aug 12, 2005.

  1. Nutter macrumors 6502

    Joined:
    Mar 31, 2005
    Location:
    London, England
    #1
    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!
     
  2. rinseout macrumors regular

    Joined:
    Jan 20, 2004
    #2
    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.
     
  3. Nutter thread starter macrumors 6502

    Joined:
    Mar 31, 2005
    Location:
    London, England
    #3
    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.
     
  4. rinseout macrumors regular

    Joined:
    Jan 20, 2004
    #4
    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?
     
  5. Nutter thread starter macrumors 6502

    Joined:
    Mar 31, 2005
    Location:
    London, England
    #5
    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!
     
  6. rinseout macrumors regular

    Joined:
    Jan 20, 2004
    #6
    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).
     
  7. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #7
    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.
     
  8. Nutter thread starter macrumors 6502

    Joined:
    Mar 31, 2005
    Location:
    London, England
    #8
    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...
     
  9. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #9
    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().
     
  10. Nutter thread starter macrumors 6502

    Joined:
    Mar 31, 2005
    Location:
    London, England
    #10
    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.
     

Share This Page