Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 9, 2009, 01:56 AM   #1
chrono1081
macrumors 604
 
chrono1081's Avatar
 
Join Date: Jan 2008
Location: Isla Nublar
Can someone explain to me exactly what is going on here?

I wanted to check the apple documentation but apples site doesn't play well with my work proxy : /


I'm coming from C++ and learning Objective-C and I was working through an exercise in a book and I realize that a line in the exercise doesn't do what I thought it did. Here is the program: (I'll just write the important parts)

First here is part of the fraction class implementation file, notice it creates a memory leak.

Code:
-(Fraction *) add: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];

//other stuff here

return result;
}
Now here is part of main.

Code:
//At the top of main we have this here creating a new fraction object
Fraction *aFraction = [[Fraction alloc] init];
Fraction *sum = [[Fraction alloc] init], *sum2;

//stuff here, skipping down to the for loop

for (i = 1; i <= n; ++i)
{
[aFraction setTo: 1 over: pow2];

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

//Here it is....
sum = sum2;
I thought that when I released it I would be destroying the object but right underneath the release of sum it gets used again without being reallocated. Am all I doing is clearing the data stored in sum? Would I need to use some type of dealloc keyword to destroy the object?
__________________
Mac Pro (2010): 3.33Ghz Intel Xeon (6 core) - 24 GB RAM - NVidia Quadro k5000
Macbook Air (2010): 2.13 Ghz Intel Core 2 Duo - 4GB RAM
chrono1081 is offline   0 Reply With Quote
Old Jul 9, 2009, 02:32 AM   #2
ChOas
macrumors regular
 
Join Date: Nov 2006
Location: The Netherlands
Quote:
Originally Posted by chrono1081 View Post
First here is part of the fraction class implementation file, notice it creates a memory leak.
Not really... You return a pointer to the allocated memory, so you are free to release the memory later. It is not a real leak. (you didn't lose the pointer to the memory)

Quote:
Code:
//At the top of main we have this here creating a new fraction object
Fraction *aFraction = [[Fraction alloc] init];
Fraction *sum = [[Fraction alloc] init], *sum2;

//stuff here, skipping down to the for loop

for (i = 1; i <= n; ++i)
{
[aFraction setTo: 1 over: pow2];

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

//Here it is....
sum = sum2;
I thought that when I released it I would be destroying the object but right underneath the release of sum it gets used again without being reallocated. Am all I doing is clearing the data stored in sum? Would I need to use some type of dealloc keyword to destroy the object?
When you alloc an object it has a retain count of 1 ... You can increase or decrease this retain count by using 'retain' or 'release'. Now, if your object receives a release when it has a retain count of 1 then it is assumed no other code is interested in the object, so the memory for the object can be de-allocated.

That is what is happening here.

1: sum2 is just a pointer to a fraction.
2: add: returns a pointer to an allocated fraction.
3: sum2 now points to the allocated fraction having a retain count of 1.
4: You release sum (retain now 0), thus deallocating the memory originally used by sum, leaving you with a usable/free pointer which you can point to any fraction again.
5: You let sum point to the same fraction as sum2 is pointing to.
ChOas is offline   0 Reply With Quote
Old Jul 9, 2009, 07:24 AM   #3
chrono1081
Thread Starter
macrumors 604
 
chrono1081's Avatar
 
Join Date: Jan 2008
Location: Isla Nublar
Thank you for the clarification

It didn't hit me looking at the exercise that the pointer was never lost. It all makes sense now
__________________
Mac Pro (2010): 3.33Ghz Intel Xeon (6 core) - 24 GB RAM - NVidia Quadro k5000
Macbook Air (2010): 2.13 Ghz Intel Core 2 Duo - 4GB RAM
chrono1081 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Can someone explain this? thedeejay iPhone 3 Apr 13, 2014 06:05 PM
How do I explain to someone that .... Adutrumque iPhone 6 Jan 19, 2013 01:25 AM
Please explain to me imjoee Mac Peripherals 3 Dec 7, 2012 01:48 PM
iPhone: Not sure how to explain this, but I need some help... ECUSnare Jailbreaks and iOS Hacks 2 Oct 1, 2012 12:14 AM
I've been gone for a while can someone please explain the changes to this forum Shenaniganz08 iPhone 2 Jul 31, 2012 05:17 AM

Forum Jump

All times are GMT -5. The time now is 03:52 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC