Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In arc what happens when you don't synthesize

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?

like image 202
1dayitwillmake Avatar asked Nov 08 '12 21:11

1dayitwillmake


3 Answers

A couple of observations, much of which is probably clear by this point based upon the feedback of others:

  1. You synthesize properties, not instance variables, and in your example, you showed us an example of an instance variable, not a property.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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).

like image 85
Rob Avatar answered Nov 15 '22 20:11

Rob


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.

like image 22
WDUK Avatar answered Nov 15 '22 19:11

WDUK


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.

like image 32
rmaddy Avatar answered Nov 15 '22 19:11

rmaddy