Struggling ALOT with memory mgmt. Please help?

Discussion in 'Mac Programming' started by czeluff, Nov 20, 2008.

  1. czeluff macrumors 6502

    Oct 23, 2006

    Hello to all, and thank you upfront for reading. I just CANNOT grasp how retain/release counts work. The link above provides you with a simple AddressCard object and does some retaining and releasing.

    I would REALLY like for someone to just walk through line by line with me, noting the retainCount of each variable along the way). For example:

    After line 1, first has a retain value of 1.
    After line 2, last has a retain value of 1

    Here's what I really just don't understand:

    1. WHY would i retain the f variable inside of setFirst? Correct me if i'm wrong, but that would increase the retainCount of f to 1. But then we never release f!!!
    2. WHY would i do [first release]? It seems to me that the retainCount PRIOR to this call = 1; calling release would make it 0, and would deallocate it. Since it's been deallocated, line #3 (first = f) would become impossible.

    Please help????:confused:
  2. czeluff thread starter macrumors 6502

    Oct 23, 2006

    nevermind, i get it all now :)

    if anyone is struggling to understand how it works, feel free to add me to AIM and we can ichat back n forth to make it simple: text, voice, or screenshare acceptable.

    AIM: neokekambas
  3. HiRez macrumors 603


    Jan 6, 2004
    Western US
    Just for reference, don't always accept what -retainCount: gives you as accurate. There's some weird things that the Cocoa runtime does under the hood with memory management that can lead -retainCount: to give a different number than expected. For example, certain things like integers 12 and under Cocoa considers constants, and will not let the retain count go to 0. Also, anytime you are dealing with autoreleased objects, you can't really predict when an object will actually be sent a release message. It could be different times in different runs of the same app.

Share This Page