use finalize for removing AppleEvent handlers in GC apps?

  • In my all-GC cocoa app, I'm registering an AppleEvent handler:

    [[NSAppleEventManager sharedAppleEventManager] setEventHandler:self

    andSelector:@selector(handleGetURLAppleEvent:withReplyEvent:)
                                                    forEventClass:kInternetEventClass
                andEventID:kAEGetURL];

    It seems that before GC, you would remove this handler in the dalloc
    method. (correct me if i'm wrong there)

    - (void)dealloc {
    ...
    [[NSAppleEventManager sharedAppleEventManager]
    removeEventHandlerForEventClass:kInternetEventClass
    andEventID:kAEGetURL];
    ...
    }

    In a GC app, should you do that removal in finalize instead? or
    somewhere else? or not at all?

    thanks,

    todd ditchendorf
  • On 12/22/07 2:19 PM, Todd Ditchendorf said:

    > In my all-GC cocoa app, I'm registering an AppleEvent handler:
    >
    >
    > [[NSAppleEventManager sharedAppleEventManager] setEventHandler:self
    >
    > andSelector:@selector(handleGetURLAppleEvent:withReplyEvent:)
    > forEventClass:kInternetEventClass
    > andEventID:kAEGetURL];
    >
    >
    > It seems that before GC, you would remove this handler in the dalloc
    > method. (correct me if i'm wrong there)
    >
    > - (void)dealloc {
    > ...
    > [[NSAppleEventManager sharedAppleEventManager]
    > removeEventHandlerForEventClass:kInternetEventClass
    > andEventID:kAEGetURL];
    > ...
    > }
    >
    > In a GC app, should you do that removal in finalize instead? or
    > somewhere else? or not at all?

    Well, everything in -finalize has to be thread-safe.  Is
    NSAppleEventManager thread safe?  If not, then -finalize is a very bad
    place to do that.

    --
    ____________________________________________________________
    Sean McBride, B. Eng                <sean...>
    Rogue Research                        www.rogue-research.com
    Mac Software Developer              Montréal, Québec, Canada
  • On Jan 3, 2008, at 2:24 PM, Sean McBride wrote:
    >> It seems that before GC, you would remove this handler in the dalloc
    >> method. (correct me if i'm wrong there)
    >>
    >> - (void)dealloc {
    >> ...
    >> [[NSAppleEventManager sharedAppleEventManager]
    >> removeEventHandlerForEventClass:kInternetEventClass
    >> andEventID:kAEGetURL];
    >> ...
    >> }
    >>
    >> In a GC app, should you do that removal in finalize instead? or
    >> somewhere else? or not at all?
    >
    > Well, everything in -finalize has to be thread-safe.  Is
    > NSAppleEventManager thread safe?  If not, then -finalize is a very bad
    > place to do that.

    In general, -finalize is a very bad place to do anything but memory
    management.  And, given that GC does almost all memory management for
    you, that makes -finalize just a very bad place.  Order is undefined
    and the thread of execution may be random, too.

    Work to remove all -finalize methods from your app.  Your codebase
    will be happier for it.

    In particular, having some kind of an "I'm done with this subgraph, go
    tear down scarce or sensitive resources in an orderly fashion"
    notification will address this issue.

    b.bum
previous month december 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