maz
maz

Reputation: 2476

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 Person : NSObject {
}
@property (retain) NSString* name;
@end

Person.m

#import "Person.h"

@implementation Person
@synthesize name;
@end

Upvotes: 1

Views: 210

Answers (2)

FAsttrack
FAsttrack

Reputation: 56

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

Upvotes: 0

kennytm
kennytm

Reputation: 523304

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.

Upvotes: 2

Related Questions