elp
elp

Reputation: 8131

Difference between retain

what's the difference between these retains?:

@property (nonatomic, retain) NSString *A_StringToRetain;

and

NSString *B_StringToRetain;
B_StringToRetain = [[MyClass GetStringValue] retain];

Because using property, string won't retain and using second ways, all ok, but i need to check and release to avoid leaks.


Example:
I declared in .h

NSString *A_StringToRetain;
@property (nonatomic, retain) NSString *A_StringToRetain;

in .m i use

A_StringToRetain = @"MyValue";

but when i exit from method, I lost A_StringToRetain. It's a zombie.

If i not use a property and declare a string in this way

NSString *B_StringToRetain;
B_StringToRetain = [[MyClass GetStringValue] retain];

the string is in memory.


Anyone tell me why please?
It's not the same way to alloc/retain?
What's differences?

thanks.

Upvotes: 2

Views: 123

Answers (1)

Simon Lee
Simon Lee

Reputation: 22334

You are setting the variable directly and NOT going through the property (which applies a retain)... you either need...

self.A_StringToRetain = someString;

or

[self setA_StringToRetain:someString];

The key here is the self. which means you go via the property and not directly to the ivar itself.

Upvotes: 1

Related Questions