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];
sum2 = [sum add: aFraction];
[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];
sum2 = [sum add: aFraction];
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,
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];
sum2 = [sum add: aFraction];
[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];
sum2 = [sum add: aFraction];
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,
Last edited: