Hello guys I've got a question regarding the following problem.
I have two objects: aClass and bClass. aClass shall contain bClass by using a retain property. After instanciating bClass (I'm not using a convenience allocator here, because I want to solve my problem of missunderstanding here), bClass is assigned to aClass by using a setProperty of aClass.
Throughout the process I check the retainCount of bClass by asking the reference and also by asking the through the property of aClass.
As far as I understood, the memory managemend of Objective C, the mentioned retain counts should be the same, because I'm sharing ownership of an object between a property and a reference.
It seems to me I have a major problem in understanding the memory management. Maybe someone can explain to me my mistake. Thx in advance.
Header of ClassA
@property (retain )ClassB *myProperty;
Source of ClassA
@implementation ClassA
-(id)init {
self = [super init];
if (self) {
ClassB * bClass = [[ClassB alloc] init];
NSLog(@"Temporary bClass retain count = %d", [bClass retainCount]);
self.myProperty = bClass;
NSLog(@"retain count after giving ownership to ClassA = %d", [bClass retainCount]);
[bClass release];
NSLog(@"retain count of bClass after release = %d", [bClass retainCount]);
NSLog(@"retain count of myProperty of ClassA = %d", [self.myProperty retainCount]);
}
return self;
}
The output is:
Temporary bClass retain count = 1
retain count after giving ownership to ClassA = 2
retain count of bClass after release = 1
retain count of myProperty of ClassA = 2