开发者

Managing memory in objective-C for auto properties set as (retain)

开发者 https://www.devze.com 2023-01-24 17:25 出处:网络
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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消