Re: Weak link usage? @property?
Re: Weak link usage? @property?
- Subject: Re: Weak link usage? @property?
- From: Marco Masser <email@hidden>
- Date: Tue, 22 Jul 2008 09:47:50 +0200
So what's the correct form of weak linking to avoid retain cycles?
Please be aware that "weak" has a different meaning depending on the
context (GC vs non-GC).
Docs:
Note: In memory management, a nonretained object reference is known as
a weak reference, which is something altogether different from a weak
reference in a garbage-collected environment. In the latter, all
references to objects are considered strong by default and are thus
visible to the garbage collector; weak references, which must be
marked with the __weak type modifier, are not visible. In garbage
collection, retain cycles are not a problem.
http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/chapter_6_section_8.html
Wikipedia:
Garbage collection
Objective-C 2.0 provides an optional conservative yet generational
garbage collector. When run in backwards-compatible mode, the runtime
turns reference counting operations such as "retain" and "release"
into no-ops. All objects are subject to garbage collection when
garbage collection is enabled. Regular C pointers may be qualified
with "__strong" to also trigger the underlying write-barrier compiler
intercepts and thus participate in garbage collection. A zero-ing weak
subsystem is also provided such that pointers marked as "__weak" are
set to zero when the object (or more simply GC memory) is collected.
http://en.wikipedia.org/wiki/Objective-C#Garbage_collection
Do I need to avoid any @property settings for any weak linked
objects and just deal with directly in my methods?
If you are not using the garbage collector, simply specify "assign" to
get what is considered a weakly linked object. I didn't try that but
from the above explanation, I'm pretty sure this is what you want.
Other question on @property retaining
@property (nonatomic, retain) MyClass *myClass;
-(void)holderOfMyClass:(MyClass *)myClassObject;
{
[self setMyClass: myClassObject]; // this retains
self.myClass = myClassObject; // this retains
myClass = myClassObject; // this does not retain? Seems like it
doesn't from my tests
}
The first two assignments are the same. The dot syntax simply is a
shortcut for the first line. Therefore, using "self.myClass = ..."
really invokes "[self setMyClass:...]", whereas "myClass = ..."
directly accesses the ivar and doesn't use setters at all. If you
declared a property like this, chances are good that you don't want to
do that.
Take a look at the Objective-C 2.0 language guide, specifically at the
docs about the dot syntax:
http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_2_section_3.html#/
/apple_ref/doc/uid/TP30001163-CH11-SW17
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden