Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
April 22, 2018, 09:13:07 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Memory problem created by macro sum example?  (Read 2305 times)
webwrx
Newbie
*
Posts: 41


« on: July 13, 2009, 11:13:27 PM »

Page 245 says - the following macro lets you easily create new fractions...

Code: (Objective-C)
#define MakeFract(x,y) ([[Fraction alloc] initWith: x over: y])

Then you can write expressions such as ... or even

Code: (Objective-C)
sum = [MakeFract (n1, d1) add: MakeFract (n2, f2)];

Am I correct in thinking this particular example would create a memory leak problem because you've just created two new objects without a name, therefore no way of releasing them (that I know of at this stage)?

Cheers...
Ben
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #1 on: July 14, 2009, 06:30:11 AM »

Ben,

Excellent catch!  You're right, the handles to the fractions are lost in the example.  You could define
Code: (Objective-C)
MakeFract
this way instead:

Code: (Objective-C)
#define MakeFract(x,y) ([[[Fraction alloc] initWith: x over: y] autorelease])

except autorelease really hasn't been covered by this point in the text.

Cheers,

Steve Kochan

Logged
webwrx
Newbie
*
Posts: 41


« Reply #2 on: July 14, 2009, 02:04:53 PM »

Phew! I think I'm starting to "get" all this stuff.

I've only ever had experience with BASIC previously - and that was many years ago, so some of the concepts in Obj-C are tricky for me, but I find with persistence, writing things down, identifying my weak points and re-working them over and over it all starts to make sense. I've purchased about 6 different books on iPhone development and Objective-C. Yours is the only book that I feel like I'm actually LEARNING anything useful from.

Some other books fast track you into building interfaces and copying code straight from the book - but unless you understand the code that you're copying from the book, I believe that approach is futile.

Your book is helping me understand what's going on... so thanks - and not only for the book, but also for setting up these forums and your participation and support here. I really appreciate it.

Ben
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #3 on: July 14, 2009, 08:08:23 PM »

Ben,

You're welcome!   Embarrassed

Cheers,

Steve Kochan
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 classroomM.com. All rights reserved.