问题
In an iOS ARC enabled project, what happens when I don't synthesize a property, since retain/release are disallowed?
@interface SomeClass : NSObject {
NSMutableArray* _pieces;
}
@end
What are the memory semantics of the iVar _pieces in this case?
Say I set it using, _pieces = whatever.
Is _pieces set to nil when the instance of my SomeClass is deallocated? Is _pieces stored as a weak reference? If all other objects that have retained _pieces release it, will it be null when I attempt to access it?
回答1:
A couple of observations, much of which is probably clear by this point based upon the feedback of others:
You synthesize properties, not instance variables, and in your example, you showed us an example of an instance variable, not a property.
Your question might imply some assumed connection between synthesizing and the ability to do
retain/release, but there is no such connection. The ability to doretainandreleaseis a function of whether you are using ARC or not. It has nothing to do with synthesizing properties.As others have observed, explicitly declared instance variables, such as your example, are
strongreferences, by default. So, in your example,_piecesis astrongreference.Yes, when your
SomeClassobject is deallocated, it will remove itsstrongreference to the_piecesobject. Obviously, if that's the last strong reference to the object pointed to by_piecesit will be deallocated and any otherweakreferences you have to it elsewhere will be set tonil. For a more complete discussion on the memory management, see Apple's Advanced Memory Management Programming Guide and Transitioning to ARC.You asked "If all other objects that have retained
_piecesrelease it, will it benilwhen I attempt to access it?" Obviously that would be true if_pieceswas aweakreference, but given that it's implicitly astrongreference inSomeClass, no, that is not the case.If you wanted to make
piecesa declared property, the syntax would be@property (nonatomic, strong) NSMutableArray* pieces;
The designation ofstrongvs.weak(or whatever) dictates the memory management of the property.If you declare a property, you not only no longer have to explicitly define the instance variable, but rather it is now advised that you really should not do so (because when it's synthesized, the compiler will create the ivar for you). But, if you happen to have an explicitly declared instance variable of the right name for your property, the compiler will use that for the property. But that's not only unnecessary, but also inadvisable (because if you mistype the name of the instance variable, you may unwittingly end up with two instance variables). Just let the compiler synthesize your instance variables for your properties and this potential ambiguity goes away.
The name of the instance variable that will be synthesized for a property is governed by the syntax of the property implementation directive, i.e. the
@synthesizestatement. Thus, if you have a@synthesizestatement for yourpiecesproperty of the form:@synthesize pieces;
then the instance variable will be calledpieces. But if you use the preferred@synthesizesyntax of:@synthesize pieces = _pieces;
then the instance variable name will have the preceeding underscore (which is, by convention, preferred, to avoid ambiguity in your code between properties and instance variables). And, as of Xcode 4.4, if you omit the@synthesizestatement for a@property, it will implicitly synthesize it for you with the latter syntax, i.e. the instance variable will bear the leading underscore).
回答2:
Assuming that you've not created a property that uses this, which overrides the assumed behaviour, instance variables in ARC projects are assumed strong, so the declaration really is
@interface SomeClass : NSObject {
__strong NSMutableArray* _pieces;
}
@end
So, in answer to your questions
Is _pieces set to nil when the instance of my SomeClass is deallocated?
No, but assigning an instance to it won't cause it to be deallocated.
Is _pieces stored as a weak reference?
No, it is a strong reference.
If all other objects that have retained _pieces release it, will it be null when I attempt to access it?
No, this is the same question as your first one.
回答3:
Are you declaring a property named pieces or is this a straight ivar?
If you define a property then the memory usage depends on how you define the property.
If this is a straight ivar then by default the ivar will be strong. This essentially means the ivar will properly retain and release what ever object you assign to it. You can safely use it without worrying about it.
回答4:
To the best of my knowledge, ARC will treat it as you would treat it in a manner similar to strong. When you assign to it, the incoming value will be retained and the value no longer pointed to will be released. It will only dangle if it somehow becomes over released. If you have a property declaration, ARC will abide by the rules specified there, and the accessors will be automatically synthesized for you in the form @synthesize someObject = _someObject. When the object gets deallocated, I presume the object gets sent release so that if nothing else has asserted ownership, the object pointed to by the pointer will also be deallocated.
回答5:
with the new runtime you only need @properties btw - dont declare ivars, dont synthesize
来源:https://stackoverflow.com/questions/13297829/in-arc-what-happens-when-you-dont-synthesize