memory management related question

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

  1. macrumors newbie

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

    so when i release the textField in the dealloc i have:

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


    Jul 24, 2002
    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

    Oct 18, 2007
    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. thread starter macrumors newbie

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

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

    Nov 9, 2007
    Colchester, UK
    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