(This concept is also described later in the book starting on page 172)

So resultFraction doesn't actually contain the numerator and denominator of a Fraction, but it references a place in memory that does. In the case of Program 7.5, the add: method allocates the actual Fraction object (called result inside the method) and returns a reference (pointer) to it that gets stored inside resultFraction by the program when the statement

Code: (Objective-C)

resultFraction = [aFraction add: bFraction];

gets executed. This creates the relationship depicted in the diagram above.

In Program 7.6, you end up with both sum and sum2 referencing (pointing to) the same Fraction object in memory. And you can only release that object's memory once.

I hope this helps. If not, let me know and I'll try again (although it's a royal pain trying to draw those rectangles with dashes and vertical bars!!)

I would think that you wouldn't be creating more references in memory by doing this, so later when you finally type [sum release] it releases the one and only number stored in memory. I must not be correctly understanding this memory management. Any help? Thank you!

I would think that you wouldn't be creating more references in memory by doing this, so later when you finally type [sum release] it releases the one and only number stored in memory. I must not be correctly understanding this memory management. Any help? Thank you!

Hi there,

Please note that the add: method used in program 7.6 returns the allocated memory for the result (see the add: method at the bottom of page 150). If you don't use the variables sum and sum2 like in program 7.6 to release the memory created by add:, then each time you go through the for loop, your program will leak memory. There's an explanation on page 153 (first two paragraphs) for the effect you create by your code above (cutting out the sum2 variable).

I would think that you wouldn't be creating more references in memory by doing this, so later when you finally type [sum release] it releases the one and only number stored in memory. I must not be correctly understanding this memory management. Any help? Thank you!

It took me a while to figure this out too. You are assuming that the *sum* outside of the "[" and the *sum* inside the "[" occupy the same space in memory. Remember, all that sum is, is a variable pointing to a bit of memory. So the sum of [sum add:....] points to here; (Lets just call it A).

A B|------------|--------------|

Now, when you get the return from your method, you are changing where it is pointing. Why? Because a **new** object ( lets say B ) was created in another part of your program (which it was) , and that reference was handed back to you, so now SUM is **assigned** that ref, which is B ( as we have arbitrarily decided). But, what about A? Under the rules with which you have designed your program, you agree to manage memory. Just because sum is no longer pointing at A, does not mean the system knows that it is free ( or cares ), and it will be unavailable to anything else....that's what leaking memory is. So, it is up to you to release A before you assign it to B. So, it is **not** that you cannot do it your way. It is that it will be doing something which is undesirable.Hope that helps.

I am confused as well. When the topic of memory leakage is described on p.153, the prescribed method of correctly handling leakage "is to divide the messages into two separate messages, as was done earlier in the program." S. Kochan is referring to these lines on p. 152 (I believe):

Here, as I understand it, the add method creates a new Fraction that doesn't get released in the add: method. By assigning that object to a variable name, it can be released later as is done with "[resultFraction release];" That is contrasted with [[aFraction add: bFraction ] print]; in which that new Fraction is created with the add: method but, since unnamed, not release-able.

So if I get that correctly, what is going with Program 7.6 ? sum is declared and allocated, sum2 is simply declared. Then, in the for loop, sum2 is assigned the return value of [sum add: aFraction], which as I understand it, creates an unnamed Fraction object that can't be released. Then sum2 is given the value of sum, which was just released. That is the most confusing part for me, because I would think that if sum was released, it doesn't exist for it to be assigned to sum2.

So if I get that correctly, what is going with Program 7.6 ? sum is declared and allocated, sum2 is simply declared. Then, in the for loop, sum2 is assigned the return value of [sum add: aFraction], which as I understand it, creates an unnamed Fraction object that can't be released. Then sum2 is given the value of sum, which was just released. That is the most confusing part for me, because I would think that if sum was released, it doesn't exist for it to be assigned to sum2.

Okay, so I think you understand the concept but you're not reading the code correctly. sum gets released and then sum is given the value of sum2, not the other way around as you stated. Reread the code and you'll see that's what is taking place.

Am I correct in stating that sum2's value is a memory address of an Fraction object created & returned when sum's add: method was called?

Also:sum2 received the result Fraction from sum's add: callThen sum was releasedThen sum was given the value of sum2, which points to the Fraction that was created in the add: call, why? Couldn't we have just used sum2 in place of sum for the following code and released sum2 where sum was released?

Am I correct in stating that sum2's value is a memory address of an Fraction object created & returned Yeswhen sum's add: method was called?sum2 received the result Fraction from sum's add: call YesThen sum was released CorrectThen sum was given the value of sum2, which points to the Fraction that was created in the add: call,That's right

why? Couldn't we have just used sum2 in place of sum for the following code and released sum2 where sum was released?

I'm not sure I follow this. Perhaps write out the code for me. But let me explain again: We need to keep a running total of the sum. That's done in the variable sum. Each time through the loop we add the current fraction (in aFraction) and sum together. The add: method stores the result of the addition in a new Fraction object that it creates, and returns that as the result. That result represents our new cumulative sum, which we store inside sum2. We can't store the result from the add: method directly back into sum (which is where we ultimately want to put it), because then we would lose our handle on the previous Fraction stored inside sum and we wouldn't be able to release its memory. That's a memory leak. So, we first release the old Fraction in sum, and then assign it the new one from sum2.

I'll keep trying until this is clear. It's obviously the source of confusion among many readers here.