Memory managment?

Discussion in 'iOS Programming' started by BlackWolf, Apr 15, 2009.

  1. BlackWolf macrumors regular

    Apr 9, 2009
    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:
    NSString *myString = @"whatever";
    self.myLabel.text = myString;
    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.
  2. jnic macrumors 6502a

    Oct 24, 2008
    1. Using "self" will use the setter code created by the property declaration, which will automatically increase the retain count (so you don't need to retain it again). If you remove the "self" then you'll need the retain if you want to keep it from being autoreleased.

    2. This is really two questions. Re. method parameters:

    If you allocate (for instance) an empty string, you'll get a pointer to a constant, which will have a retain count of UINT_MAX. This is an implementation detail, please don't rely on retainCount for debugging memory management. This is stated explicitly in the docs:, please read them carefully.

    3. Again, using self and then using alloc will retain your object twice.
  3. BlackWolf thread starter macrumors regular

    Apr 9, 2009
    thank you! this was really helpful, especially that "self" uses the setter method.
    so just to make it absolutly clear: if I define an object as "assign", it doesn't matter if I use self. or not. if I use retain or copy, not using "self" will be like assign, and using self will be retain or copy. right?

    two more questions regarding all this though:
    1) I'm not quite sure when to use which accessor method. right now, I use assign most of the time and retain for example if an instance variable changes really often. I use copy if I want to make sure the instance variable is only changed by the class it belongs to.
    2) I actually used retainCount a lot these past days to see if my app was behaving as I expected it to, memory-wise. I also checked out the ObjectAllocation part of instruments. while it is useful, it doesn't really help me to find small leaks, does it?

  4. jnic macrumors 6502a

    Oct 24, 2008
    Very welcome :)

    That's correct, yes. The docs have a nice breakdown of what each setter does:

    1. Depends what you're doing really, but so long as you're thinking about their behavior in terms of memory management when you code it's not hugely important. See the link above for concise descriptions of how each setter behaves.

    2. Try Clang for picking up leaks you may have missed in Instruments. I've posted about it here:
  5. BlackWolf thread starter macrumors regular

    Apr 9, 2009
    thanks. yeah, I already read about the setters and seems I got it all right. thank you so much, I'll have a look at clang :)

Share This Page