Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

czeluff

macrumors 6502
Original poster
Oct 23, 2006
272
2
http://www.otierney.net/objective-c.html#memorymanagement

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:
 

czeluff

macrumors 6502
Original poster
Oct 23, 2006
272
2
nevermind

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
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.