David is 100% correct in his answer. I just added four explicit examples using GHUnit.
The lifetime qualifier behavior for object references.
Using NSObject
as a proxy for all objects, the behavior of the lifetime qualifiers is as expected.
- (void) test_usingNSObjects
{
NSObject *value1 = [[NSObject alloc] init];
NSObject *value2 = [[NSObject alloc] init];
NSObject *value3 = [[NSObject alloc] init];
__strong NSObject *sRefToValue = value1;
__weak NSObject *wRefToValue = value2;
__unsafe_unretained NSObject *uRefToValue = value3;
value1 = value2 = value3 = nil;
GHAssertNotNil(sRefToValue,
@"Strong reference to the object that was originally \
assigned to value1. Even though value1 was set to nil, the \
strong reference to the object keeps the object from being \
destroyed.");
GHAssertNil(wRefToValue,
@"Weak reference to the object that was originally assigned to \
value2. When value2 was set to nil, the weak reference does \
not prevent the object from being destroyed. The weak \
reference is also set to nil.");
// Removing the #ifdef and #endif lines will result in a EXC_BAD_ACCESS
// signal. Receiving a EXC_BAD_ACCESS signal is the expected behavior for
// that code.
#ifdef RECIEVE_EXC_BAD_ACCESS
GHAssertNotNil(uRefToValue,
@"Unsafe unretained reference to the object that was \
originally assigned to value3. When value3 was set to nil, \
the unsafe unretained reference does not prevent the object \
from being destroyed. The unsafe unretained reference is \
unaltered and the reference is invalid. Accessing the \
reference will result in EXC_BAD_ACCESS signal.");
#endif
// To avoid future EXC_BAD_ACCESS signals.
uRefToValue = nil;
}
The lifetime qualifier behavior for literal NSString
s (@"something").
This is basically the same as test_usingNSObjects
, but instead of using a NSObject
, a NSString
that is assigned a literal string is used. Since literal strings are not destroyed like other objects, different behaviors for __weak
and __unsafe_unretained
variables are observed.
- (void) test_usingLiteralNSStrings
{
NSString *value1 = @"string 1";
NSString *value2 = @"string 2";
NSString *value3 = @"string 3";
__strong NSString *sRefToValue = value1;
__weak NSString *wRefToValue = value2;
__unsafe_unretained NSString *uRefToValue = value3;
value1 = value2 = value3 = nil;
GHAssertNotNil(sRefToValue,
@"Strong reference to the object that was originally \
assigned to value1. Even though value1 was set to nil, \
literal strings are not destroyed.");
GHAssertNotNil(wRefToValue,
@"Weak reference to the object that was originally assigned \
to value2. Even though value2 was set to nil, \
literal strings are not destroyed so the weak reference is \
still valid.");
GHAssertNotNil(uRefToValue,
@"Unsafe unretained reference to the object that was \
originally assigned to value3. Even though value3 was set \
to nil, literal strings are not destroyed so the unsafe \
unretained reference is still valid.");
}
The lifetime qualifier behavior for nonliteral NSString
s.
This is basically the same as test_usingNSObjects
, but instead of using a NSObject
, a NSString
that is assigned a nonliteral string is used. Since the nonliteral strings are destroyed like other objects, the behaviors are the same as observed in test_usingNSObjects
.
- (void) test_usingNonliteralNSStrings
{
NSString *value1 = [[NSString alloc] initWithFormat:@"string 1"];
NSString *value2 = [[NSString alloc] initWithFormat:@"string 2"];
NSString *value3 = [[NSString alloc] initWithFormat:@"string 3"];
__strong NSString *sRefToValue = value1;
__weak NSString *wRefToValue = value2;
__unsafe_unretained NSString *uRefToValue = value3;
value1 = value2 = value3 = nil;
GHAssertNotNil(sRefToValue,
@"Strong reference to the object that was originally \
assigned to value1. Even though value1 was set to nil, the \
strong reference to the object keeps the object from being \
destroyed.");
GHAssertNil(wRefToValue,
@"Weak reference to the object that was originally assigned to \
value2. When value2 was set to nil, the weak reference does \
not prevent the object from being destroyed. The weak \
reference is also set to nil.");
// Removing the #ifdef and #endif lines will result in a EXC_BAD_ACCESS
// signal. Receiving a EXC_BAD_ACCESS signal is the expected behavior for
// that code.
#ifdef RECIEVE_EXC_BAD_ACCESS
GHAssertNotNil(uRefToValue,
@"Unsafe unretained reference to the object that was \
originally assigned to value3. When value3 was set to nil, \
the unsafe unretained reference does not prevent the object \
from being destroyed. The unsafe unretained reference is \
unaltered and the reference is invalid. Accessing the \
reference will result in EXC_BAD_ACCESS signal.");
#endif
// To avoid future EXC_BAD_ACCESS signals.
uRefToValue = nil;
}
NSString
creation - literal vs nonliteral.
Shows strings created in various ways if they are literal nor nonliteral.
- (void) test_stringCreation
{
NSString *literalString = @"literalString";
NSString *referenced = literalString;
NSString *copy = [literalString copy];
NSString *initWithString = [[NSString alloc] initWithString:literalString];
NSString *initWithFormat = [[NSString alloc] initWithFormat:@"%@", literalString];
// Testing that the memory addresses of referenced objects are the same.
GHAssertEquals(literalString, @"literalString", @"literal");
GHAssertEquals(referenced, @"literalString", @"literal");
GHAssertEquals(copy, @"literalString", @"literal");
GHAssertEquals(initWithString, @"literalString", @"literal");
GHAssertNotEquals(initWithFormat, @"literalString",
@"nonliteral - referenced objects' memory addresses are \
different.");
// Testing that the objects referenced are equal, i.e. isEqual: .
GHAssertEqualObjects(literalString, @"literalString", nil);
GHAssertEqualObjects(referenced, @"literalString", nil);
GHAssertEqualObjects(copy, @"literalString", nil);
GHAssertEqualObjects(initWithString, @"literalString", nil);
GHAssertEqualObjects(initWithFormat, @"literalString", nil);
// Testing that the strings referenced are the same, i.e. isEqualToString: .
GHAssertEqualStrings(literalString, @"literalString", nil);
GHAssertEqualStrings(referenced, @"literalString", nil);
GHAssertEqualStrings(copy, @"literalString", nil);
GHAssertEqualStrings(initWithString, @"literalString", nil);
GHAssertEqualStrings(initWithFormat, @"literalString", nil);
}