Cocoa Why do I need to store and release a function parameter?

I am working on a book by Aaron Hilllegass, specifically on the lottery example. I have a question about the method -setEntryDate:

; why should i keep date

? The program still works without saving it.

 -(void)setEntryDate:(NSCalendarDate *)date {
    [date retain];
    [entryDate release];
    entryDate = date;
}

      

But this still works great:

-(void)setEntryDate:(NSCalendarDate *)date {
    entryDate = date;
}

      

So why is it right that I need to keep date

and then release entryDate

?

0


a source to share


4 answers


This now works, but if you are writing a larger program, chances are that at some undefined point in the future, the object date

indicates that it will be released by anyone setEntryDate

. If this happens, it will be invalidated throughout the rest of the program. You are storing this object in the class, because this class now has a reference to this object and must indicate this. By doing this, even if the class setEntryDate

had to freedate

, your class would still retain a valid reference to it. Also, it's not just the usual old method you write. It is a setter that has a special responsibility to set an instance variable in the class it belongs to. If you write a method without a setter, you may not need to store parameters. What I'm trying to say is that the parameters of the backing method are not always necessary; it's easy in this case (and pretty much all setters that deal with non-primitive types).



This is called "reference counting" and is explained in detail here . For now, since you are just starting to learn, don't worry about reading this anymore. As you start getting into more complex memory management scenarios, this guide is a very valuable guide.

+7


a source


Because you are claiming ownership of the object, and retain

this is how you do it. Sometimes the wrong code works right, but it's essentially just luck.



See Cocoa's memory management rules .

+3


a source


Methods like these are called accessor methods. They, as the name suggests, allow you to get variables and are set - specifically, they are called "getters" and "setters".

The convention (which you will, however, see in later chapters of the book, is more than a convention) is to call a "getter" on a variable, for example NSString

calledfoo

- (NSString*)foo;

      

And "setter":

- (void)setFoo:(NSString*)newFoo;

      

In the above example, the method is implemented to set a new date value. Memory management is covered in Chapter 4, but, in short, the way Objective-C works is that they have a "persistence count" - this is the number of references an object has; when allocated, the objects have a retention counter of 1. The objects can then be sent messages retain

or release

to increase or decrease the retention value, respectively. A retain

means that the object sending the message wants to use the object, so retain

it is. Messagerelease

means the entity sending the message no longer wants to use the entity, so it reduces the number of holds. When the hold value of the object reaches 0, the object is released. This prevents memory leaks.

The reason it is date

kept and entryDate

released is because date

- this is a new object that you want to "know" about; you therefore claim to own it retain

. The variable entryDate

points to the current date object, but since you are setting it to a new value, you no longer need to know about it; so you release

it; this prevents memory leaks as you originally saved this variable.

As I said, once you read Chapter 4: Memory Management, the concept will become much clearer. For now, just accept that and understand the reasoning behind when it is explained.

+1


a source


If you learn better from the videos, Stanford University has published several videos on iPhone development that also cover Cocoa and Objective-C to some extent. Read Lecture 3 for a good overview of memory management, with examples and discussion.

+1


a source







All Articles