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?
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 do retain
and release
is 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 strong
references, by default. So, in your example, _pieces
is a strong
reference.
Yes, when your SomeClass
object is deallocated, it will remove its strong
reference to the _pieces
object. Obviously, if that's the last strong reference to the object pointed to by _pieces
it will be deallocated and any other weak
references you have to it elsewhere will be set to nil
. 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 _pieces
release it, will it be nil
when I attempt to access it?" Obviously that would be true if _pieces
was a weak
reference, but given that it's implicitly a strong
reference in SomeClass
, no, that is not the case.
If you wanted to make pieces
a declared property, the syntax would be @property (nonatomic, strong) NSMutableArray* pieces;
The designation of strong
vs. 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 @synthesize
statement. Thus, if you have a @synthesize
statement for your pieces
property of the form:@synthesize pieces;
then the instance variable will be called pieces
. But if you use the preferred @synthesize
syntax 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 @synthesize
statement for a @property
, it will implicitly synthesize it for you with the latter syntax, i.e. the instance variable will bear the leading underscore).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With