简体   繁体   中英

Objective-C Beginner (release) question

I'm just starting to learn Objective-C. I read the Cocoa Become an XCoder book, and I think I learned the basics. Now, I'm following an online tutorial where I encountered this bit of code:

@synthesize name;

- (IBAction)changeGreeting:(id)sender {
self.name = textInput.text;

NSString *nameString = name;
if([nameString length] == 0) {
    nameString = @"Cartman";
}
NSString *greeting = [[NSString alloc] 
                      initWithFormat:@"Hello, my name is %@!", nameString];
label.text = greeting;
[greeting release];
}

My question here is, shouldn't we call 'release' also on the *nameString variable? Or by doing that I would clean also the 'name' property which should be released in the 'dealloc' method? Cause if I understand correctly, I must call 'release' on all variables located inside functions at the end of those functions, but on the class properties I must call 'release' only in the 'dealloc' method?

Thanks

Release only the objects you claim ownership on. This means every property you set to retain or copy. Don't release assigned properties.

You claim ownership by sending alloc, copy, new or mutableCopy. Have a look at Apple's Memory Management Programming Guide / Object Ownership and Disposal . You should also release if you retain manually by sending retain.

Regarding this you don't have to release nameString.

greeting is released because you allocated it. nameString doesn't need to be released because it is an assignment. You have to release an object that you alloc as a general rule of thumb.

See the great Apple Memory Management Guide for further help. Memory management is a big hurdle for lots of iOS beginners, and the memory management guide should basically be required reading.

Also, in this specific example, you don't really need the nameString variable, you could just use self.name everywhere it is used.

I ran your code through Google Translate's Obj-C to English filter:

@synthesize name;

Dear compiler: please create a setter and a getter method for this property called name on my behalf.

self.name = textInput.text;

Take the object that's in textInput.text and use the previously-created setter method to put it into my property called name .

NSString *nameString = name;

Make a new variable, nameString , and point it at the same object that name points at. (I'm not doing anything that affects the memory management of that object; I just want a new label that I can use to refer to the object by.)

nameString = @"Cartman";

Make my variable nameString point at this other object, a literal string.

(This does not change the object that name points to:
If name -> OBJECT ,
after nameString = name , then also nameString -> OBJECT ,
but if then nameString = OTHER_OBJECT , still name -> OBJECT .)

NSString *greeting = [[NSString alloc] 
                  initWithFormat:@"Hello, my name is %@!", nameString];

Grab a chunk of memory the correct size for an NSString object, and set up that memory with the following arguments:.... Point the variable greeting at that object. Since I explicitly asked for this memory using alloc , I am responsible for it. When I no longer need it, I will release the object and free up the memory.

label.text = greeting;

Take the object that greeting points at, and put it in label 's property called text . label should do whatever memory management it needs on the object; that part isn't my problem.

[greeting release];

I have no further need for that chunk of memory that I created. Get rid of it, post haste.

English is quite a bit wordier than Objective-C. :)

The nameString variable is simply a pointer to whatever is stored in your class property of name. So that string is technically stored in a memory block that nameString just points to. When your method ends, the nameString pointer is simply cleared from memory. However, name will still be pointing at that memory. If you release nameString, it will clear the memory that name also points to and will therefore give you issues later if you try to access the memory associated with name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM