views:

77

answers:

3

Hello,

As everyone knows, setTitle automatically retains the string passed as a parameter. When the button caption needs to be changed, I guess that it is necesary to release the current (old) string before setting the new one. I wonder what is the most elegant way to dot it.

See my code sample (here, getPlayerHandFromGame method produces autoreleased strings which are retained when setTitle is invoked):

colourString = [pGame getPlayerHandFromGame:1 withColour:COLOUR_HEARTS];


// Split colourString into array of strings if not null.
    if ([colourString length] != 0) {
        listCards = [colourString componentsSeparatedByString:@" "];
        for (cardCounterSameColour = 1; cardCounterSameColour <= [listCards count]; cardCounterSameColour ++) {
            currentCardButton = [self buttonCardNumber:cardCounter];
            // Objects are numbered from 0 in the array
            [currentCardButton setTitle:[listCards objectAtIndex:cardCounterSameColour-1] forState:UIControlStateNormal];
            cardCounter ++;
        }
    }

This portion of code will be called several times since the button caption will be updated several times. I guess that before setting the title, I should do something like this:

[currentCardButton titleForState:UIControlStateNormal release]

in order to release the string which will not be used anymore (titleForState returns a pointer to the NSString).

Is that the right way to avoid that the device memory gets loaded with unused strings ?

Many thanks, Apple92

+6  A: 

Any class that retains a value set on one of its properties is also responsible for releasing the old value when that property's value gets changed again. Don't worry about it.

Noah Witherspoon
+2  A: 

Take a look at the object ownership convention:

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmObjectOwnership.html#//apple_ref/doc/uid/20000043-BEHDEDDB

Every class should follow these rules (Apple's does it), so in order to them you dont have to worry because you haven't done a alloc/retain in your class, and the button will retain it for it's internal use.

Angel García Olloqui
+2  A: 

As everyone knows, setTitle automatically retains the string passed as a parameter.

Really? I don't know it. In fact, I'll bet a pint of beer that it doesn't retain the string, but copies it.

Granted, for an NSString, -copy is probably implemented as doing a retain and returning self, but if you pass it an NSMutableString, a genuine copy will occur.

I guess that it is necesary to release the current (old) string before setting the new one.

Guess again, sucker!

Or less facetiously: any object is responsible for managing the ownership of other objects it wantsa to keep hold of. Once you have passed the title in setTitle: you do not need to worry about how the object disposes of it once it gets a new one.

Consider the code:

[currentCardButton titleForState:UIControlStateNormal]

and apply the memory management rules to the return result.

Did you obtain it with alloc, new or copy? No. Did you retain it? No (remember we are talking about the object passed back by the method,not the object you originally gave it). Therefore, you must not release it.

JeremyP