Hi there,
I have the following example class. The Apple documentation states that retain, copy and assign attributes to property only affects the setters.
https://developer.apple.com/iphone/....html#//apple_ref/doc/uid/TP30001163-CH17-SW2
However I have noticed the retaincount is increased everytime I make a call to a getter.
#import <Cocoa/Cocoa.h>
@interface Person : NSObject {
@private NSString *name;
@private NSNumber *age;
}
@property (readwrite, retain) NSString *name;
@property (readwrite, retain) NSNumber *age;
+ (id) create: (NSString *) name andAge: (NSNumber *) age ;
- (id)init;
- (void) printState;
@end
with the implementation
#import "Person.h"
@implementation Person
@synthesize name;
@synthesize age;
- (id) init
{
if ( self = [super init] )
{
[self setName
""];
[self setAge:nil];
}
return self;
}
+ (id) create: (NSString *) name andAge: (NSNumber *) age
{
Person* person = [[[Person alloc] init] autorelease];
[person setName: name];
[person setAge:age];
return person;
}
- (NSNumber *) age
{
return self->age;
}
- (void) printState
{
NSLog(@"Name of person is %@ age %@",[self name],[self age]);
NSLog(@"Retain count for name and age is %lu, %lu",[name retainCount],[age retainCount]);
}
- (void) dealloc
{
[self setAge:nil];
[self setName:nil];
[super dealloc];
}
@end
main.m
#import <Cocoa/Cocoa.h>
#import "Person.h"
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *name = [NSString stringWithString: @"Fred"];
NSNumber *age = [NSNumber numberWithInt:30];
Person *person = [Person create:name andAge:age];
[person printState];
[person printState];
[person printState];
[pool release];
}
Nov 18 07:23:22 Byron MyTool[2074]: Name of person is Fred age 30
Nov 18 07:23:22 Byron MyTool[2074]: Retain count for name and age is 2147483647, 3
Nov 18 07:23:22 Byron MyTool[2074]: Name of person is Fred age 30
Nov 18 07:23:22 Byron MyTool[2074]: Retain count for name and age is 2147483647, 4
Nov 18 07:23:22 Byron MyTool[2074]: Name of person is Fred age 30
Nov 18 07:23:22 Byron MyTool[2074]: Retain count for name and age is 2147483647, 5
If I supply an implementation that reflects a simply return:
- (NSNumber *) age
{
return self->age;
}
then output is:
ov 18 07:27:38 Byron MyTool[2122]: Name of person is Fred age 30
Nov 18 07:27:38 Byron MyTool[2122]: Retain count for name and age is 2147483647, 2
Nov 18 07:27:38 Byron MyTool[2122]: Name of person is Fred age 30
Nov 18 07:27:38 Byron MyTool[2122]: Retain count for name and age is 2147483647, 2
... and continues the same
therefore I believe the code generated for a retain or copy for a getter must be sometime like:
- (NSNumber *) age
{
return [self->age retain];
}
Can anyone confirm this behaviour? Should I be concerned or will I have to place an autorelease on every getter call.
(Note: I have disabled GC in Xcode)
I have the following example class. The Apple documentation states that retain, copy and assign attributes to property only affects the setters.
https://developer.apple.com/iphone/....html#//apple_ref/doc/uid/TP30001163-CH17-SW2
However I have noticed the retaincount is increased everytime I make a call to a getter.
#import <Cocoa/Cocoa.h>
@interface Person : NSObject {
@private NSString *name;
@private NSNumber *age;
}
@property (readwrite, retain) NSString *name;
@property (readwrite, retain) NSNumber *age;
+ (id) create: (NSString *) name andAge: (NSNumber *) age ;
- (id)init;
- (void) printState;
@end
with the implementation
#import "Person.h"
@implementation Person
@synthesize name;
@synthesize age;
- (id) init
{
if ( self = [super init] )
{
[self setName
[self setAge:nil];
}
return self;
}
+ (id) create: (NSString *) name andAge: (NSNumber *) age
{
Person* person = [[[Person alloc] init] autorelease];
[person setName: name];
[person setAge:age];
return person;
}
- (NSNumber *) age
{
return self->age;
}
- (void) printState
{
NSLog(@"Name of person is %@ age %@",[self name],[self age]);
NSLog(@"Retain count for name and age is %lu, %lu",[name retainCount],[age retainCount]);
}
- (void) dealloc
{
[self setAge:nil];
[self setName:nil];
[super dealloc];
}
@end
main.m
#import <Cocoa/Cocoa.h>
#import "Person.h"
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *name = [NSString stringWithString: @"Fred"];
NSNumber *age = [NSNumber numberWithInt:30];
Person *person = [Person create:name andAge:age];
[person printState];
[person printState];
[person printState];
[pool release];
}
Nov 18 07:23:22 Byron MyTool[2074]: Name of person is Fred age 30
Nov 18 07:23:22 Byron MyTool[2074]: Retain count for name and age is 2147483647, 3
Nov 18 07:23:22 Byron MyTool[2074]: Name of person is Fred age 30
Nov 18 07:23:22 Byron MyTool[2074]: Retain count for name and age is 2147483647, 4
Nov 18 07:23:22 Byron MyTool[2074]: Name of person is Fred age 30
Nov 18 07:23:22 Byron MyTool[2074]: Retain count for name and age is 2147483647, 5
If I supply an implementation that reflects a simply return:
- (NSNumber *) age
{
return self->age;
}
then output is:
ov 18 07:27:38 Byron MyTool[2122]: Name of person is Fred age 30
Nov 18 07:27:38 Byron MyTool[2122]: Retain count for name and age is 2147483647, 2
Nov 18 07:27:38 Byron MyTool[2122]: Name of person is Fred age 30
Nov 18 07:27:38 Byron MyTool[2122]: Retain count for name and age is 2147483647, 2
... and continues the same
therefore I believe the code generated for a retain or copy for a getter must be sometime like:
- (NSNumber *) age
{
return [self->age retain];
}
Can anyone confirm this behaviour? Should I be concerned or will I have to place an autorelease on every getter call.
(Note: I have disabled GC in Xcode)