Weak connections (without garbage collection)

Discussion in 'Mac Programming' started by RossOliver, Jul 3, 2008.

  1. RossOliver macrumors regular

    Joined:
    Nov 6, 2006
    #1
    Hey,

    I understand what a weak connection is, but I'm having trouble creating one between a class and its delegate. My set up is as follows:

    Class:

    Code:
    
    instance var:
    id<ATMyDelegate> *_delegate;
    
    initializer:
    -( id ) initWithDelegate:( id * )delegate
    {
    ...
    _delegate = delegate;
    ...
    }
    
    usage:
    [_delegate myDelegateMethod:self];
    
    properties:
    @property( nonatomic, assign ) id	*_delegate;
    @synthesize _delegate = delegate;
    
    

    Delegate:

    Code:
    
    initialize class:
    [class initWithDelegate:self];
    
    dealloc:
    [class setDelegate:nil];
    [class release];
    
    
    To me this should be correct - the only thing I am unsure about is the @property being 'assign' (I know it shouldn't be retain)...

    Can anyone spot where I have gone wrong?

    Thanks for your time,

    -Ross
     
  2. ElectricSheep macrumors 6502

    ElectricSheep

    Joined:
    Feb 18, 2004
    Location:
    Wilmington, DE
    #2
  3. RossOliver thread starter macrumors regular

    Joined:
    Nov 6, 2006
    #3
    I was under the impression this was just in a garbage-collection environment? Only I am programming for the iphone OS...

    Is the rest of my set up accurate?

    Cheers,

    -Ross

    Having read through that document it appears a __weak quantifier shields the field from the garbage collector - since the iPhone OS has no garbage collection does this mean that all references are weak by default? Or does the __weak quantifier suppress an automatic retain call or something? Utterly confusing myself now :s
     
  4. ElectricSheep macrumors 6502

    ElectricSheep

    Joined:
    Feb 18, 2004
    Location:
    Wilmington, DE
    #4
    Prior to the introduction of Garbage Collection, a weak reference was simply one that was not retained (does not increase the referred objects retain count). Since the assign attribute of your @property performs a simple assignment without altering the retain count of the referred object, this creates a weak reference. I don't see anything that would prevent the delegate object from being released from memory.
     
  5. RossOliver thread starter macrumors regular

    Joined:
    Nov 6, 2006
    #5
    Ah ok, so the initial:

    Code:
    _delegate = delegate;
    
    Assignment doesn't increase the delegate retain count when it is initialized with __weak, but it would otherwise?
     
  6. ElectricSheep macrumors 6502

    ElectricSheep

    Joined:
    Feb 18, 2004
    Location:
    Wilmington, DE
    #6
    It shouldn't increase the retain count either way. If it were:

    Code:
    _delegate = [delegate retain];
    
    Then the retain count is incremented and a strong reference is created.
     
  7. RossOliver thread starter macrumors regular

    Joined:
    Nov 6, 2006
    #7
    Ah, well maybe I'm doing something wrong then. What's the correct way to define pointer parameters as local instance variables - retain them or simply assign them? I guess you should retain them in case the object that passes them in releases them before you are finished with them?

    -Ross
     
  8. ElectricSheep macrumors 6502

    ElectricSheep

    Joined:
    Feb 18, 2004
    Location:
    Wilmington, DE
    #8
    If you didn't get the object via alloc, new, copy, or mutableCopy you should retain it and then release it when you are finished. Unless, of course, you don't particularly care if the object is deallocated. You can send messages to nil, but you cannot send messages to a deallocated object so you'd have to make sure that pointers to the dealloc'ed object get cleared at the time of deallocation.
     

Share This Page