NSTask, alloc but no release?

  • Does NSTask (sometimes) violate the general rule that, if you -alloc
    an object, you must then -release it?  The example below allocates an
    NSTask and registers for a termination notification.  When the
    notification is received, it releases the NSTask.  Since the NSTask
    was -alloc'ed, that would seem to be necessary, but doing so makes the
    app crash.

    It looks as though the NSTask is getting autoreleased, but where?  I'd
    expect that if I was using -launchedTaskWithLaunchPath:arguments:, but
    not for -alloc.  The NSTask documentation doesn't give any hints that
    -release is a bad idea here, but apparently it is.

    - (void)taskComplete:(NSNotification *)note
    {
    NSTask *theTask = [note object];
    NSLog(@"termination status: %d", [theTask terminationStatus]);
    [theTask release];
    }

    - (IBAction)runTask:(id)sender
    {
    NSTask *theTask = [[NSTask alloc] init];
    [theTask setLaunchPath:@"/bin/ls"];
    [theTask setArguments:[NSArray arrayWithObjects:@"-lR", @"/private/tmp", nil]];
    [[NSNotificationCenter defaultCenter] addObserver:self
          selector:@selector(taskComplete:)
          name:NSTaskDidTerminateNotification
          object:theTask];
    [theTask launch];
    }

    --
    Tom Harrington
    <atomicbird...>
    AIM: atomicbird1
  • On Oct 1, 2007, at 9:37 AM, Tom Harrington wrote:
    > - (void)taskComplete:(NSNotification *)note
    > {
    > NSTask *theTask = [note object];
    > NSLog(@"termination status: %d", [theTask terminationStatus]);
    > [theTask release];
    > }
    > - (IBAction)runTask:(id)sender
    > {
    > NSTask *theTask = [[NSTask alloc] init];
    > [theTask setLaunchPath:@"/bin/ls"];
    > [theTask setArguments:[NSArray arrayWithObjects:@"-lR", @"/private/
    > tmp", nil]];
    > [[NSNotificationCenter defaultCenter] addObserver:self
    > selector:@selector(taskComplete:)
    > name:NSTaskDidTerminateNotification
    > object:theTask];
    > [theTask launch];
    > }

    It likely doesn't explain the crash, but a quick read of your code
    indicates that you need to remove 'self' as an observer of the
    'NSTaskDidTerminateNotification' notification prior to releasing the
    task.

    NSNotificationCenter is not self cleaning, nor does it retain all
    objects that are involved in notifications.  Not removing
    notification observers is a very large source of crashers in Cocoa
    applications.

    b.bum
  • On 10/1/07, Bill Bumgarner <bbum...> wrote:
    > It likely doesn't explain the crash, but a quick read of your code
    > indicates that you need to remove 'self' as an observer of the
    > 'NSTaskDidTerminateNotification' notification prior to releasing the
    > task.

    Actually that does seem to explain the crash, because if I add this
    the crash disappears.  Thanks, that really helps.

    --
    Tom Harrington
    <atomicbird...>
    AIM: atomicbird1
previous month october 2007 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today