hi,
I was just reading through the basic memory managment things in cocoa touch, but I'm afraid I'm not quite getting it. A few questions regarding memory managment:
1. What if I want to assign a string to a UILabel's text property? As far as I understood it, I would have to retain that:
Since myString is autoreleased, wouldn't it be deallocated at one point or another? So do I have to do self.myLabel.text = [myString retain];? I know I do NOT have to retain it, but I don't quite get why. Is it because text is nonatomic, copy? Does that mean that it automatically copies every value assigned to it?
2. What about method parameters? If I have an NSString object as a method parameter, do I need to release it or is it released automatically at the end of the method? this especially confuses me, since the behaviour seems to be different in different situations. I have some method parameters that come with a retain count of 1, some that come with a retain count of 2 (I guess because I screwed something up in memory managment before) and some that come with the (insane) retain count of 2147483647 (which is, I guess, the highest possible int) ... whatever that's supposed to mean.
3. I have a custom class Deck that is a subclass of NSObject. Why the hell, when I do
self.something = [[Deck alloc] init];
somewhere to create a deck object, does this deck object have a retain count of 2 right after the line I just posted? It's just been initialized, how is that possible? shouldn't it have a retain count of 1?
there are some more things in my code that are confusing me about memory managment, but I guess when I understood the above points the rest will clear up. I can get my code to work with try&error and I guess I understood the basic things about memory managment already, but there are quite a few situations where I don't really understand the behaviour of my code.
I was just reading through the basic memory managment things in cocoa touch, but I'm afraid I'm not quite getting it. A few questions regarding memory managment:
1. What if I want to assign a string to a UILabel's text property? As far as I understood it, I would have to retain that:
Code:
NSString *myString = @"whatever";
self.myLabel.text = myString;
2. What about method parameters? If I have an NSString object as a method parameter, do I need to release it or is it released automatically at the end of the method? this especially confuses me, since the behaviour seems to be different in different situations. I have some method parameters that come with a retain count of 1, some that come with a retain count of 2 (I guess because I screwed something up in memory managment before) and some that come with the (insane) retain count of 2147483647 (which is, I guess, the highest possible int) ... whatever that's supposed to mean.
3. I have a custom class Deck that is a subclass of NSObject. Why the hell, when I do
self.something = [[Deck alloc] init];
somewhere to create a deck object, does this deck object have a retain count of 2 right after the line I just posted? It's just been initialized, how is that possible? shouldn't it have a retain count of 1?
there are some more things in my code that are confusing me about memory managment, but I guess when I understood the above points the rest will clear up. I can get my code to work with try&error and I guess I understood the basic things about memory managment already, but there are quite a few situations where I don't really understand the behaviour of my code.