Basic question about RELEASE in Obj-C

chuckbo

Registered
I know this must be a very basic question, but I'm having trouble understanding something I read in the Objective-C book. I have a lot of background in Visual Basic and RealBasic, so maybe that's getting in the way. And I've been reading various articles and searching for the last hour but haven't found anything that really helps.

Regarding memory management, what does the release command really do? I'm looking at a loop in this snippet
pow2 = 2;
for (i=1; i<=n; ++i) {
[aFraction setTo: 1 over: pow2];
sum2 = [sum add: aFraction];
[sum release]; // release previous sum
sum = sum2;
pow2 *= 2;
}

What confuses me is the reference to sum right after it's been released. When I first read the chapter, the idea associated it with is that the object has been "released" and isn't available anymore -- but then the very next line uses it again. But if the object is still hanging around after the Release, then what did the Release do/accomplish?

Chuck
 
Every Objective-C object has a retain count. The release command reduces the object's retain count by 1. When the retain count goes down to 0, the object is not available anymore.

Unless there was a retain command (or some other command that increases the object's retain count) before the code snippet you posted, the sum object should be unavailable after the release. Check the add: method to see if it does anything to increase sum's retain count.
 
Back
Top