Will dealloc
(below) release the NSString
pointed to by the static variable exampleString
?
// ExampleClass.h
@interface ExampleClass : NSObject
@end
// ExampleClass.m
static NSString *exampleString;
@implementation ExampleClass
- (void)dealloc {
exampleString = nil;
}
- (id)init {
self = [super init];
if (self) {
exampleString = [NSString stringWithFormat:@"example %@", @"format"];
}
return self;
}
@end
Yes, because since you did not specify an ownership qualifier, the LLVM compiler infers that exampleString
has __strong
ownership qualification.
This means that by setting exampleString
to nil
in dealloc
, you are retaining nil
(the new value), which does nothing, and releasing the old value.
Source
According to section 4.4.3. Template arguments of the LLVM documentation on Objective-C Automatic Reference Counting (ARC), "If a template argument for a template type parameter is an retainable object owner type that does not have an explicit ownership qualifier, it is adjusted to have __strong
qualification."
And, according to section 4.2. Semantics, "For __strong
objects, the new pointee is first retained; second, the lvalue is loaded with primitive semantics; third, the new pointee is stored into the lvalue with primitive semantics; and finally, the old pointee is released. This is not performed atomically; external synchronization must be used to make this safe in the face of concurrent loads and stores.