Other Programming Languages
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreOther Programming Languages

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 18th, 2010, 04:45 PM
thomashw thomashw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Location: Alberta
Posts: 6 thomashw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 19 m 3 sec
Reputation Power: 0
Other Language - Objective-C - Understanding "release"

I've bolded the part of the code I don't fully understand.
  • Won't sum = sum2 just overwrite the old sum? So why do you need to release it beforehand?
  • Why isn't sum2 released at the end of the program? Should it be?

Code:
#import “Fraction.h”

int main (int argc, char *argv[])
{

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction *aFraction = [[Fraction alloc] init]; Fraction *sum = [[Fraction alloc] init], *sum2; int i, n, pow2;

[sum setTo: 0 over: 1]; // set 1st fraction to 0

NSLog (@”Enter your value for n:”);
scanf (“%i”, &n);
pow2 = 2;

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

NSLog (@”After %i iterations, the sum is %g”, n, [sum convertToNum]); [aFraction release];
[sum release];
[pool drain];

return 0;
}

Reply With Quote
  #2  
Old July 19th, 2010, 11:52 PM
thomashw thomashw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Location: Alberta
Posts: 6 thomashw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 19 m 3 sec
Reputation Power: 0
Bump.

Reply With Quote
  #3  
Old July 21st, 2010, 04:15 PM
OmegaZero OmegaZero is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2007
Posts: 737 OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 23 h 15 m 48 sec
Reputation Power: 928
You'll probably get more/better answers in the C forum.

I don't know Objective-C, but (assuming alloc/release == new/delete) it sounds like you're confusing references with values. "sum" and "sum2" aren't actual Fraction objects, but are just the address of Fraction objects. So "sum = sum2" only makes "sum" and "sum2" refer to the same Fraction object. The one "sum" originally referenced would still be out there in memory (hence the release). You don't need to release "sum2" at the end of the program since after the loop ends both "sum" and "sum2" refer to the same Fraction object, calling release using "sum2" would be an error since the Fraction object it refers to was already released when you called release using "sum".
__________________
sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);

Reply With Quote
  #4  
Old August 23rd, 2010, 04:53 PM
TheOtherDino's Avatar
TheOtherDino TheOtherDino is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2010
Location: Katy, Texas
Posts: 489 TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 4 Days 18 h 25 m 29 sec
Reputation Power: 198
Quote:
Originally Posted by thomashw
I've bolded the part of the code I don't fully understand.
  • Won't sum = sum2 just overwrite the old sum? So why do you need to release it beforehand?
  • Why isn't sum2 released at the end of the program? Should it be?

Code:
#import “Fraction.h”

int main (int argc, char *argv[])
{

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction *aFraction = [[Fraction alloc] init]; Fraction *sum = [[Fraction alloc] init], *sum2; int i, n, pow2;

[sum setTo: 0 over: 1]; // set 1st fraction to 0

NSLog (@”Enter your value for n:”);
scanf (“%i”, &n);
pow2 = 2;

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

NSLog (@”After %i iterations, the sum is %g”, n, [sum convertToNum]); [aFraction release];
[sum release];
[pool drain];

return 0;
}


Since sum and sum2 are both pointers to instances of Fractions, all
Code:
sum = sum2 ; 

will do is overwrite the pointer contained in sum, thus causing a memory leak to the instance that sum used to point to. You need to release it before the assignment of sum2. Remember, the setTo method in Fraction.h will return a pointer to a new instance of Fraction.

sum2 could have been released at the end of the program, as both sum and sum2 point to the same instance.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreOther Programming Languages > Other Language - Objective-C - Understanding "release"

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap