Skip navigation.
 
mlRe: Garbage collection - was Beginner with Cocoa
FROM : Clark Cox
DATE : Mon Mar 24 17:45:24 2008

On Mon, Mar 24, 2008 at 9:12 AM, Bill Cheeseman <<email_removed>> wrote:
> on 2008-03-24 11:30 AM, colo at <email_removed> wrote:
>
>  > In Ruby GC just works dandy without thought. Why is it so different in
>  > Cocoa Obj2.0?
>
>  My main issue is how to know exactly when to declare instance variables weak
>  or strong.


You rarely need to worry about weak instance variables.

> There seem to be some subtleties in that area that I don't yet
>  understand. On the other hand, it appears that these issues will rarely be
>  encountered, and that the simple rules of thumb in the documentation will
>  almost always be enough.


Indeed; it is very rare that you need to declare an instance variable
weak. Most of the reasons that, under retain/release, one might
intentionally not retain an instance variable (conceptually similar to
"weak" under GC) are already handled by the collector. That is, under
retain/release, when making a parent/child relationship, one would
have to be careful to only retain *one* of the pointers defining the
relationship in order to avoid circular references.

Under GC, there is no need to worry about this, as the collector is
smart enough to recognize that a pair of objects (or a whole tree of
objects) can be collected, as long as nothing outside of that tree has
references to any of the objects inside of it.

For example, under retain/release:
  @class Child;
  @interface Parent : NSObject { Child *child; }
  @property (retain) Child *child;
  @end

  @interface Child : NSObject { Parent *parent; }
  //We can't retain the parent pointer without introducing a circular reference
  @property (assign) Child *child;
  @end

  @implementation Parent
  @dynamic child;

  -(void)dealloc {
    self.child = nil;
    [super dealloc];
  }

  -(void)setChild:(Child*)c {
    if(c != child) {
      child.parent = nil;
      c.parent    = self;
      [child release];
      child = [c retain];
    }
  }

  -(Child*)child {
    return child;
  }

  @end

  @implementation Child
  @synthesize parent;
  @end

Under GC, this becomes (note that there is no need for "weak"):
  @class Child;
  @interface Parent : NSObject { Child *child; }
  @property Child *child;
  @end

  @interface Child : NSObject { Parent *parent; }
  @property Child *child;
  @end

  @implementation Parent
  @dynamic child;

  -(void)setChild:(Child*)c {
    if(c != child) {
      child.parent = nil;
      c.parent    = self;
      child = c;
    }
  }

  -(Child*)child {
    return child;
  }

  @end

  @implementation Child
  @synthesize parent;
  @end


--
Clark S. Cox III
<email_removed>

Related mailsAuthorDate
mlBeginner with Cocoa Alex Handley Mar 21, 20:00
mlRe: Beginner with Cocoa Jeff LaMarche Mar 21, 20:21
mlRe: Beginner with Cocoa Jack Repenning Mar 24, 05:16
mlRe: Beginner with Cocoa Sherm Pendley Mar 24, 05:44
mlRe: Beginner with Cocoa Jeff LaMarche Mar 24, 13:12
mlRe: Beginner with Cocoa Erik Buck Mar 24, 15:13
mlRe: Beginner with Cocoa Scott Thompson Mar 24, 15:32
mlGarbage collection - was Beginner with Cocoa Bill Cheeseman Mar 24, 15:57
mlRe: Garbage collection - was Beginner with Cocoa colo Mar 24, 16:30
mlRe: Garbage collection - was Beginner with Cocoa Bill Cheeseman Mar 24, 17:12
mlRe: Garbage collection - was Beginner with Cocoa Erik Buck Mar 24, 17:12
mlRe: Garbage collection - was Beginner with Cocoa Clark Cox Mar 24, 17:45
mlRe: Beginner with Cocoa Jack Repenning Mar 24, 18:01
mlRe: Garbage collection - was Beginner with Cocoa colo Mar 24, 18:02
mlRe: Garbage collection - was Beginner with Cocoa Scott Thompson Mar 24, 18:36
mlRe: Beginner with Cocoa colo Mar 24, 18:43
mlRe: Garbage collection - was Beginner with Cocoa Bill Cheeseman Mar 24, 18:45
mlRe: Beginner with Cocoa Jeff LaMarche Mar 24, 18:54
mlRe: Beginner with Cocoa Sherm Pendley Mar 24, 18:57
mlRe: Beginner with Cocoa Chris Hanson Mar 24, 19:39
mlRe: Beginner with Cocoa Clark Cox Mar 24, 19:44
mlRe: Garbage collection - was Beginner with Cocoa colo Mar 24, 21:03
mlRe: Garbage collection - was Beginner with Cocoa has Mar 24, 22:59
mlRe: Garbage collection - was Beginner with Cocoa Scott Thompson Mar 25, 03:15
mlRe: Beginner with Cocoa Scott Ribe Mar 25, 21:57
mlRe: Garbage collection - was Beginner with Cocoa Scott Ribe Mar 25, 22:06
mlRe: Beginner with Cocoa Bill Cheeseman Mar 25, 22:40
mlRe: Garbage collection - was Beginner with Cocoa Jack Repenning Mar 25, 23:01
mlRe: Garbage collection - was Beginner with Cocoa Nick Zitzmann Mar 25, 23:07
mlRe: Garbage collection - was Beginner with Cocoa Paul Bruneau Mar 25, 23:11
mlRe: Garbage collection - was Beginner with Cocoa Randall Meadows Mar 25, 23:15
mlRe: Garbage collection - was Beginner with Cocoa Rob Napier Mar 26, 00:02
mlRe: Garbage collection - was Beginner with Cocoa Jack Repenning Mar 26, 00:28
mlRe: Garbage collection - was Beginner with Cocoa colo Mar 26, 00:52
mlRe: Garbage collection - was Beginner with Cocoa Erik Buck Mar 26, 01:38
mlRe: Garbage collection - was Beginner with Cocoa mmalc crawford Mar 26, 06:24