• 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 in separate thread is leaking memory
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSTask in separate thread is leaking memory


  • Subject: NSTask in separate thread is leaking memory
  • From: Oleg Krupnov <email@hidden>
  • Date: Wed, 4 Feb 2009 12:49:32 +0200

I use an NSTask to collect system info from system_profiler. Because
it takes a while to complete, I launch that task in a separate thread,
to avoid UI from freezing.

Here is my code (I borrowed it from JRFeedbackProvider,
http://github.com/rentzsch/jrfeedbackprovider/tree/master):

- (void)gatherSystemInfo
{
	[NSThread detachNewThreadSelector:@selector(systemProfilerThread:)
                             toTarget:self
                           withObject:nil];
}

- (void)systemProfilerThread:(id)ignored
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSString *configuration = nil;

	NSPipe* inputPipe = [NSPipe pipe];
	NSPipe* outputPipe = [NSPipe pipe];

	NSTask* scriptTask = [[[NSTask alloc] init] autorelease];

	[scriptTask setLaunchPath:@"/usr/sbin/system_profiler"];
	[scriptTask setArguments:[NSArray arrayWithObjects:@"-detailLevel",
@"mini", nil]];
	[scriptTask setStandardOutput:outputPipe];
	[scriptTask launch];

	[[inputPipe fileHandleForWriting] closeFile];
	configuration = [[[NSString alloc] initWithData:[[outputPipe
fileHandleForReading] readDataToEndOfFile]
										   encoding:NSUTF8StringEncoding] autorelease];
	if (!m_isCanceled)
	{
		[m_target performSelectorOnMainThread:m_action
withObject:configuration waitUntilDone:NO];
	}

    [pool drain];
}

The problem is, according to the Leaks tool, is that there's a memory
leak in -systemProfilerThread. The leak disappears if I launch the
task in the main thread.

I've read in docs that NSTask is NOT thread safe, but does it also
apply to the above case, when the NSTask object is created and
released within a single thread, though not the main thread? If NSTask
cannot be used this way, then which way I should use it?

Also, what puzzles me is why, first and foremost, the main thread is
blocked until the task is complete? I do not call wait -waitUntilExit.
I would expect that the main thread is not blocked, but I receive the
NSTaskDidTerminateNotification, but it does not happens with the code
above.
_______________________________________________

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 in separate thread is leaking memory
      • From: Ken Thomases <email@hidden>
  • Prev by Date: Re: NSService - I need a headslap
  • Next by Date: Empty window appears when going fullscreen in awakeFromNib
  • Previous by thread: Re: NSService - I need a headslap
  • Next by thread: Re: NSTask in separate thread is leaking memory
  • Index(es):
    • Date
    • Thread