Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

RossOliver

macrumors regular
Original poster
Nov 6, 2006
157
0
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
 

RossOliver

macrumors regular
Original poster
Nov 6, 2006
157
0
You may find this helpful:

http://developer.apple.com/document...cAPI.html#//apple_ref/doc/uid/TP40002467-SW14

Two extra keywords have been introduced to help you clarify if a reference should be weak or strong, they are __strong and __weak. You use the keyword when declaring your instance variables.

Code:
__weak id<ATMyDelegate> *_delegate

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
 

ElectricSheep

macrumors 6502
Feb 18, 2004
498
4
Wilmington, DE
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

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.
 

RossOliver

macrumors regular
Original poster
Nov 6, 2006
157
0
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).

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?
 

ElectricSheep

macrumors 6502
Feb 18, 2004
498
4
Wilmington, DE
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?

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.
 

RossOliver

macrumors regular
Original poster
Nov 6, 2006
157
0
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.

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
 

ElectricSheep

macrumors 6502
Feb 18, 2004
498
4
Wilmington, DE
I guess you should retain them in case the object that passes them in releases them before you are finished with them?

-Ross

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.