andrewz
andrewz

Reputation: 5220

Retain count of allocated object assigned to a retained property

In the following code I am expecting the retain count to increase to 2, but it remains at 1 after assignment. The assignment is to a property with a retain qualifier. A retain will increment the retain count of the object by 1. Can anyone explain why the retain count is not incremented?

MyClass.h:

@property (nonatomic,retain) UIImage * imageBackground;

MyClass.m:

UIImage * IMAGE = [[UIImage alloc] initWithContentsOfFile:@"image.png"];
NSLog(@"retain-count(%d)", [IMAGE retainCount]); // returns 1
imageBackground = IMAGE;
NSLog(@"retain-count(%d)", [IMAGE retainCount]); // returns 1, should return 2

Upvotes: 0

Views: 394

Answers (1)

kennytm
kennytm

Reputation: 523294

self.imageBackground = IMAGE;

Without the self. you are not using the property's setter, and thus the retain count won't change since that was just a simple pointer assignment to an ivar.

Upvotes: 2

Related Questions