Managing memory in objective-C for auto properties set as (retain)
If I'm not using garbage collection and I have a auto property set as retain. Should I release the object in my dealloc or does the generated code handle that for me.
More clearly, will the following code leak memory if I don't release name
in dealloc.
Person.h
@interface Perso开发者_StackOverflow中文版n : NSObject {
}
@property (retain) NSString* name;
@end
Person.m
#import "Person.h"
@implementation Person
@synthesize name;
@end
The retain
/assign
/copy
attributes of a @property
only affects how they would behave in the getter and setter. You do need to manually -release
the ivar in -dealloc
.
yes this will create memory leak.
You should release it manually or in dealloc to prevent memory leak.
Here is another way to work with this..
//.h file
@interface Person : NSObject {
NSString* name;
}
@end
//.m file
import "Person.h"
@implementation Person
-(void)ViewdidLoad
{
//You can use this by self.name=[Nsstring StringWithFormat:@""];
} @end
Thanks Shyam Parmar
精彩评论