Remaining with issue?

I'm just wondering, do I need to add more [release name] elsewhere to match the save here in the getter?

- (NSString *)name {
    return [[name retain] autorelease];
}

      

Gary

+1


source to share


7 replies


I don't know how your variable definition is in your class, but the rule is that in your receiver, you should return an object unchanged for the reference count. This is the caller's answer to the call if he wants to keep a link to it.

- (NSString*) name {
  return name;
}

// caller
NSString* name = object.name;
[name retain]; // if necessary. If the string is used only in the context of a method you do not have to retain it.

      

If you are using the return value as a field in another class, you must define your field like this:



@property(retain, nonatomic) NSString* name;

      

In this case, saving will be called when assigning a variable.

+2


source


No, but you don't need to do this at all as you are not highlighting anything. You can just return the name and everything should be fine. Was there a reason you needed to add this retention / auto ad?



A bit more explanation of what's going on here is that your save rate increases by one when you execute retain

, and then down by 1 when the area exists due autorelease

.

+3


source


No, that's fine. an auto-abstract will cause the value to be released when the current autoresist pool is merged.

Each save must match exactly one of the releases or auto advertisements.

However, I believe that both the preservation and the abstract are not needed here. Usually you want to use this autorelease idiom because you have highlighted something in that method.

+2


source


Not. autorelease

will balance it. However, I do not think that retain

and are needed autorelease

. You can just use return name

.

+1


source


As others have said, you don't need to save or auto-update property. Since the callers of the "getter" method did not create the object, they do not own it, and you can safely assume that they will not bother with their save account.

However , callers can potentially change the value of the variable returned by the receiver, which will affect the object. So it would probably be better to return a copy of your variable, especially since it's an NSString. (Getters for NSString objects often return a copy.)

- (NSString *)name {
    return [[name copy] autorelease];
}

      

In this case, you are making a copy of the variable, so you have it. By auto-implementing it before it is returned, you ensure that it survives long enough to be used in the scope of the call, and that any changes they make to the variable β€œname” will not affect the underlying object.

+1


source


I'm just wondering, do I need to add more [release name] elsewhere to match the save here in the getter?

- (NSString *)name {
    return [[name retain] autorelease];
}

      

No, because you are already releasing it. autorelease

simply means "send yourself release

later."

I think you should consider the rules for memory management .

+1


source


I think I could figure it out:

if [myString] is created outside of a method then your safe to use ...

return myString;

      

if on the other hand [myString] is created inside a method and therefore needs to be freed and returned, then you use.

myString = [[NSString alloc] initWithFormat: @"Send me home"]; 
return [myString autorelease];

      

So the method sets [myString] to autorelease, basically creates an object, sets it to autorelease and returns. The object will eventually be released when the pool is destroyed.

0


source







All Articles