Garbage Collection and NSNetService

  • Hello!

    I'm trying to wrap my head around Garbage Collection, but I'm baffled
    by the behavior of some of my objects. For example, I am trying to
    publish a Bon Jour service. I have an object that does this,
    BJController. It is instantiated within the Nib and the service is
    published during awakeFromNib

    In my .h file, I have:

    @property (retain) NSNetService *service;

    and in the .m file:

    @synthesize service;

    When I publish the service I use:

    self.service = [[NSNetService alloc] initWithDomain:@""
    type:@"_BJTest._tcp" name:@"Just a Test" port:5260];

    [self.service setDelegate:self];
    [self.service publish];

    Now, as long as I keep Garbage Collection turned off, this works fine.
    When I turn it on, however, the service object seems to evaporate
    before the service is published, and my NSNetService delegate methods
    never get called at all.

    I've been through the GC docs, and I'm assuming it's got something to
    do with a weak reference somewhere. But I can't seem to figure out
    where that would be.

    Any ideas?

    Thanks!

    August
  • On Nov 11, 2007, at 8:37 AM, August Trometer wrote:

    > I have an object that does this, BJController. It is instantiated
    > within the Nib and the service is published during awakeFromNib
    > [...]
    > Now, as long as I keep Garbage Collection turned off, this works
    > fine. When I turn it on, however, the service object seems to
    > evaporate before the service is published, and my NSNetService
    > delegate methods never get called at all.
    > I've been through the GC docs, and I'm assuming it's got something
    > to do with a weak reference somewhere. But I can't seem to figure
    > out where that would be.
    >
    <http://developer.apple.com/documentation/Cocoa/Conceptual/GarbageCollection
    /Articles/gcEssentials.html
    >

    mmalc
  • Hey August -

    NIB loading leaves a retain of +1 on all top level NIB objects. That's
    what is keeping your object alive without GC. With GC, that retain is
    a no-op. In order to keep your object alive you'll need to do
    something like reference it from another live object, reference it
    from a global, or CFRetain/CFRelease it.

    Good Luck -
    Jon Hess

    On Nov 11, 2007, at 8:37 AM, August Trometer <bluegus...> wrote:

    > Hello!
    >
    > I'm trying to wrap my head around Garbage Collection, but I'm
    > baffled by the behavior of some of my objects. For example, I am
    > trying to publish a Bon Jour service. I have an object that does
    > this, BJController. It is instantiated within the Nib and the
    > service is published during awakeFromNib
    >
    > In my .h file, I have:
    >
    > @property (retain) NSNetService *service;
    >
    > and in the .m file:
    >
    > @synthesize service;
    >
    >
    > When I publish the service I use:
    >
    >
    > self.service = [[NSNetService alloc] initWithDomain:@""
    > type:@"_BJTest._tcp" name:@"Just a Test" port:5260];
    >
    > [self.service setDelegate:self];
    > [self.service publish];
    >
    >
    > Now, as long as I keep Garbage Collection turned off, this works
    > fine. When I turn it on, however, the service object seems to
    > evaporate before the service is published, and my NSNetService
    > delegate methods never get called at all.
    >
    > I've been through the GC docs, and I'm assuming it's got something
    > to do with a weak reference somewhere. But I can't seem to figure
    > out where that would be.
    >
    > Any ideas?
    >
    > Thanks!
    >
    > August
  • Thanks Jon, moving the object out of the Nib did the trick.

    -- August

    On Nov 11, 2007, at 12:52 PM, Jon Hess wrote:

    > Hey August -
    >
    > NIB loading leaves a retain of +1 on all top level NIB objects.
    > That's what is keeping your object alive without GC. With GC, that
    > retain is a no-op. In order to keep your object alive you'll need to
    > do something like reference it from another live object, reference
    > it from a global, or CFRetain/CFRelease it.
    >
    > Good Luck -
    > Jon Hess
previous month november 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    
Go to today