Question about Object Creation

Discussion in 'iOS Programming' started by mk-argon, Aug 11, 2008.

  1. mk-argon macrumors newbie

    Jun 7, 2007

    I'm relatively new to Obj-C as a language and I have decided to turn my hand to developing for the iPhone. At present I am going through the motions of creating lots of different 'Hello World' type apps to try and get a good feel for the language, having only really used interpreted (and therefore not memory managed) languages before I am still getting to grips with the retain/release strategy within Obj-C.

    I have created an app, which among other things has a label filled with text. I then have a UIPickerView which calls the following method to redefine the UILabel textColor property.

    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    	if(pickerView == textColorPicker) {
    		if(currentColor != nil) {
    			[currentColor release];
    		currentColor = [[textColorObjects objectAtIndex:row] retain];
    		UIColor *newTextColor = [UIColor colorWithRed:[[currentColor objectAtIndex:0] floatValue] green:[[currentColor objectAtIndex:1] floatValue] blue:[[currentColor objectAtIndex:2] floatValue] alpha:1];		
    		textLabel.textColor = newTextColor;
    		[newTextColor release];
    textColorObjects is an NSArray of NSArrays where each object defines the R/G/B values of the color.

    The problem is that this code crashes as soon as I change the value of the picker. I have traced it back to what I believe is `newTextColor` being totally released by the end of the method. If I remove [newTextColor release]; the code works perfectly, but the variable held by textLabel.textColor and newTextColor maintains a retainCount of 2, so even when it is released as a property of textLabel, the object remains in memory.

    I then changed the assignment of newTextColor to use [[UIColor alloc] initWithRed:....]; and re-added the release line. Everything then works correctly with a retainCount of 1 being maintained.

    What is the difference between calling +colorWithRed:Green:Blue:Alpha: and alloc...-initWithRed:Green:Blue:Alpha: I was under the impression that they both returned, ultimately the same object. Both returned objects certainly have a retainCount of 1 as I have checked. Is it that +colorWithRed:... is autoreleased?

    I hope you are able to help me and thank you for your time.


  2. robbieduncan Moderator emeritus


    Jul 24, 2002
    There is a basic rule in Cocoa. alloc/init (and copy) return objects that have been retained. All other methods return objects that are autoreleased. You do not need to release an autoreleased object, but if you want it to stick around you need to retain it.

    In your particular example you do not need to retain it as the textLabel.textColor = newTextColor call will retain it. You do not need to release it as it's autoreleased.

    I'd recommend reading this and/or this
  3. mk-argon thread starter macrumors newbie

    Jun 7, 2007
    Thanks so much for your help. I suspected that autoreleasing would be the problem but couldn't really find any references.

    I appreciate your prompt response and will definitely have a read of those links,


Share This Page