Hi,
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.
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.
Thanks
Andrew
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.
Code:
- (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.
Thanks
Andrew