memory management related question

Discussion in 'iOS Programming' started by spire.bt, Aug 30, 2010.

  1. spire.bt macrumors newbie

    Joined:
    Apr 8, 2010
    #1
    I have a text field and i manually (by code) set a font and text color for it

    Code:
    textFiled.textColor
    textField.font
    
    so when i release the textField in the dealloc i have:

    Code:
    [textField release];
    textField = nil;
    should i have a
    Code:
    [textField.font release];
    [textField.textColor release];
    
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Absolutely not. If you have retained those objects then you may need to release them but you should never release objects owned/managed by another object. That will cause crashes.
     
  3. RonC macrumors regular

    Joined:
    Oct 18, 2007
    Location:
    Chicago-area
    #3
    In general, let the owning object clean itself up...

    Think of it this way. You have a reference to a Thingy instance, say thingy. You don't really know what's inside a Thingy object, but thingy does. Let the Thingy do it's own cleaning up; it should know how to do that and it should be implemented in Thingy's dealloc method.

    So, in your example, you have an instance textField (sans typo). Simply release it and when the final reference to that object is released, the system will do the right thing - it will take care of calling the object's dealloc method when the reference count hits 0.

    See Apple's Memory Management Programming Guide for more information.

    Ron C
     
  4. spire.bt thread starter macrumors newbie

    Joined:
    Apr 8, 2010
    #4
    I had to ask...sorry if it was dumb
    thx for the reply :D

    btw what's SANS typo
     
  5. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #5
    Do read the memory management guide as the rules are quite straightforward once you know them (there are a few tricky edge cases but they are just that...edge cases).

    Also, you don't need to nil out your variables in the dealloc method once you've released them; releasing them is enough. The only time you might want to nil out a variable after releasing is in places other than dealloc, where something might want to try and access that variable again after it has been released (in which case, not setting it to nil will cause a crash when its accessed).
     

Share This Page