Sample program from book, alleged memory leakage that I'm just not seeing.

Discussion in 'Mac Programming' started by MrEcted, Apr 21, 2011.

1. Apr 21, 2011
Last edited: Apr 21, 2011

MrEcted macrumors newbie

Joined:
Apr 21, 2011
#1
Hello everyone,

I'm a .NET developer by trade, but I'm trying to learn Objective-C so I can have a little fun programming for the iPhone.

I have a little issue here, I'm going through the book 'Programming in Objective-C 2.0' by Kochan and I just finished building Program 7.6.

In the book he states that if an object isn't released during a for-loop, then a memory leakage would be created and if there were thousands of iterations it would have created a memory leakage mess. The issue is that I simply don't see the leakage problem.

I'll only give the snippet in question. There is a Fraction object which has an add method, which returns a Fraction object containing the sum of the called Fraction plus a Fraction that was passed. No big deal. Here's the snippet in question

for(i = 1; i <= n; ++i) {
[aFraction SetTo:1 over: pow2];
[sum release];
sum = sum2;
pow2 *= 2;
}

In the book he says there could be a catastrophic memory leakage if the line
[sum release]
weren't in the for-loop.

I don't get that because the very next line just assigns sum to sum2. Why couldn't I just do this -

for(i = 1; i <= n; ++i) {
[aFraction SetTo:1 over: pow2];
sum = sum2;
pow2 *= 2;
}
[sum release];

I just don't see how sum is creating an accumulating leakage problem because it's reassigned, can't I just release it after the for loop? Does assigning sum to sum2 without releasing sum first create some sort of dangling pointer problem where there is still a pointer pointing at the sum memory address? That's the only thing I can think of, but I want to be sure I fully understand this.

Any insight is greatly appreciated.

Thanks much,

2. jamawa macrumors newbie

Joined:
Jul 7, 2009
#2
Have a look at what the 'add:' method returns. Note that this exercise changed the method from returning nothing (and working on it's own variables) to returning something.
Study the altered code of the method (a couple of pages earlier) and I guess you'll see what happens. It is not so much related to the for-loop itself as it is to the add: method.

Have fun!

3. jiminaus macrumors 65816

Joined:
Dec 16, 2010
Location:
Sydney
#3
sum points to an object. That object exists independently to the variable sum.

sum2 also points to an object and that object also exists independently to sum2.

An object variable in Objective-C is just holds the memory address where the actual bytes of the object are. When sum2 is assigned to sum, all that happens in the address that's in sum2 is copied into sum.

So before the assignment, sum points to one object. After the assignment, sum points to another object. If sum was only variable in your program that had the address of that first object, then after the assignment you've effectly lost that object. But it still exists, occupying memory.

Without the release in your loop, on every iteration you'll loose yet another object. If the loop iterates 1000 times, they'll be 1000 (or maybe 999) objects that you'll have lost and that will needlessly occupy memory. And because you've lost the addresses of these object, you can never send them the last release message, and so the memory they occupy will continue to be occupied until your program terminates.

4. MrEcted thread starter macrumors newbie

Joined:
Apr 21, 2011
#4
Thanks for the help guys! That makes sense now.

5. skochan macrumors regular

Joined:
Apr 1, 2006
Location:
California
#5
You can also go to the forum for the book at classroomM.com/objective-c for these types of questions. In fact, you'll find a detailed discussion there on this particular example.

Cheers,

Steve Kochan

6. chrono1081 macrumors 604

Joined:
Jan 26, 2008
Location:
Isla Nublar
#6
I remember being stuck on the same example trying to figure it out! I'm glad its in there because it taught me to look more closely at things.

I really have to go through that book again. I haven't gotten to touch Objective-C for about a year and a half now