On Nov 16, 2010, at 09:17, Jon Gilkison wrote:

> Below is a simple test application that launches a process and logs the 
> output as it runs.
> 
> It works as expected when the app is set to no garbage collection, but as 
> soon as I turn on garbage collection, the following notifications stop 
> working:
> 
> - NSTaskDidTerminateNotification
> - NSFileHandleReadCompletionNotification

You can figure this out by working backwards.

With GC enabled, the default notification center maintains only a weak 
reference to observers registered to it. So, if you're not getting 
notifications then it's likely the observer has been garbage collected too soon.

Since the notification center isn't responsible for maintaining a strong 
reference to your observer object, what is? Well, nothing. You allocate the 
object in the app delegate, but you don't stash the result anywhere.

That means your object is subject to garbage collection at *any* time after its 
creation. When that happens depends on when the GC thread actually runs.

Likely the problem is somewhat masked in this case, because you're starting a 
non-Cocoa process, which is going to keep executing until either it finishes 
normally, or it crashes because a resource you allocated for it disappears 
after garbage collection. If you haven't seen a crash yet you've possibly just 
been lucky.

Note that your code was always wrong because you were leaking the observer 
object. It just didn't matter until GC "fixed" the leak for you.

So the short answer is probably that your application delegate should keep a 
strong reference to your observer object until the external task ends.


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to