• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSTask, alloc but no release?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSTask, alloc but no release?


  • Subject: NSTask, alloc but no release?
  • From: "Tom Harrington" <email@hidden>
  • Date: Mon, 1 Oct 2007 10:37:12 -0600

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
email@hidden
AIM: atomicbird1
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: NSTask, alloc but no release?
      • From: Bill Bumgarner <email@hidden>
  • Prev by Date: Re: Where are the boundaries of Cocoa and how are the boundaries perceived ?
  • Next by Date: Re: NSTask, alloc but no release?
  • Previous by thread: Re: Where are the boundaries of Cocoa and how are the boundaries perceived ?
  • Next by thread: Re: NSTask, alloc but no release?
  • Index(es):
    • Date
    • Thread