Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Releasing objects, reusing objects

Releasing objects, reusing objects
Thread Tools
Fresh-Faced Recruit
Join Date: Sep 2003
Status: Offline
Reply With Quote
Feb 8, 2004, 08:46 AM
 
I'm currently in the process of teaching myself Objective-C; so far no bumps in the road, with the exception of the following. Here is some code:

int main (int argc, char *argv[])
{
Fraction *aFraction = [[Fraction alloc] init]
Fraction *sum = [[Fraction allow] init], *sum2;
int i;

...

for (i=1; i<=n; ++i) {
[aFraction setTo: 1 over: 16]
sum2 = [sum add: aFraction];
[sum free];
sum = sum2;
}

.....

[aFraction free];
[sum free];

return 0;
}

Here are parts of the interface:

-(void) setTo: (int) n over: (int) d; // sets numerator and denominator
-(Fraction *) add: (Fraction *) f; // adds one fraction to another

So, what I am not entirely clear about is the role of "[sum free]". Here is what I see going on. At the start, we create objects aFraction and sum. The add: method creates an object of type Fraction, and returns that object. The object that is returned is assigned to sum2 (by "sum2 = [sum add: aFraction]").

Here is where I get confused. "[sum free]" releases sum; so that object no longer exists. Now, I think "sum = sum2" makes sum point to the same object as sum2. Then on the next run through the loop, sum2 gets the new object, the object to which sum points is released, and so on. If we did not do this, we could end up with a large number of unreleased objects ... correct?

Also, if we had the following,

sum2 = [sum add: aFraction];
sum = sum2;
[sum2 free];

would this mean that sum2 would be released, but that sum would simultaneously be released (since sum and sum2 refer to the same object)?

Thanks for the help.
     
Addicted to MacNN
Join Date: May 2001
Location: Cupertino, CA
Status: Offline
Reply With Quote
Feb 8, 2004, 12:57 PM
 
You should be using [sum release], not sure why you're using free.

The way you think it works above seems to be correct, but it's not the usual way of doing things in objective c. Generally, a function that creates a new object and returns it would autorelease that object. So this function:

-(Fraction *) add : (Fraction *) f; // adds one fraction to another

Could look like this:

Code:
- (Fraction *)add : (Fraction *)f { result = [[[Fraction alloc] init] autorelease]; ..... // calculations return result; }
If you're unfamiliar with autorelease pools, I'd recommend taking a look at this documentation. The reason you need to use autorelease is because, from the function's point of view, you want to return an object but you don't want to retain it. It's the receiving object's job to retain the object if it wants to keep it. As a result, your for loop can be like this:

Code:
[aFraction setTo: 1 over: 16]; for (i=1; i<=n; ++i) { sum = [sum add: aFraction]; } ... // do other calculations [aFraction release]; return 0;
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:57 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2