I've created a Person class, of which I instantiate two objects:
int main(int argc, const char * argv[])
{
@autoreleasepool {
Person * __weak pweak = [Person new];
Person *p = [Person personWithName:@"Strong" lastName:nil dateOfBirth:nil];
}
return 0;
}
The Person
class overrides its dealloc
method, so that it prints the name of the Person
being deallocated.
Everything goes as expected, the weak variable doesn't keep the Person
instance alive, I see this in the log ("John" is the default name of a Person
object):
2013-01-23 17:36:51.333 Basics[6555:303] John is being deallocated
2013-01-23 17:36:51.335 Basics[6555:303] Strong is being deallocated
However, if I use the factory method in the assignation to the weak variable:
int main(int argc, const char * argv[])
{
@autoreleasepool {
Person * __weak pweak = [Person personWithName:@"Weak" lastName:nil dateOfBirth:nil];
Person *p = [Person personWithName:@"Strong" lastName:nil dateOfBirth:nil];
}
return 0;
}
this is what I see logged:
2013-01-23 17:44:16.260 Basics[6719:303] Strong is being deallocated
2013-01-23 17:44:16.262 Basics[6719:303] Weak is being deallocated
Am I doing something wrong?
These methods of the Person class may be concerned:
- (id)initWithName:(NSString *)name lastName:(NSString *)lastName dateOfBirth:(NSDate *)birth {
self = [super init];
if (self) {
_name = name;
_lastName = lastName;
_dateOfBirth = birth;
}
return self;
}
+ (id)personWithName:(NSString *)name lastName:(NSString *)lastName dateOfBirth:(NSDate *)birth {
return [[self alloc] initWithName:name lastName:lastName dateOfBirth:birth];
}