Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4231208
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T01:56:40+00:00 2026-05-21T01:56:40+00:00

I recently started learning Objective-C 2.0, with a book, and I want to know

  • 0

I recently started learning Objective-C 2.0, with a book, and I want to know if I got this concept right.

So here is the code, which causes an error for releasing an object that was not allocated:

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    Fraction *aFraction = [[Fraction alloc] init];
    Fraction *sum = [[Fraction alloc] init], *sum2;
    int n, i, pow2;

    [sum setTo: 0 over: 1];

    NSLog (@"Enter a 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];
        sum = sum2;
        pow2 *= 2;
    }

    NSLog (@"After %i iterations, the sum is %g and the fraction is %i/%i.", n, [sum convertToNum], [sum numerator], sum.denominator);
    [aFraction release];
    [sum release];

    [pool drain];
    return 0;
}

I am wondering about sum and sum2. Here is the method add:

- (Fraction *) add: (Fraction *) f
{
    Fraction *resultFraction = [[Fraction alloc] init];
    int resultNum, resultDenom;
    resultNum = numerator * f.denominator + denominator * f.numerator;
    resultDenom = denominator * f.denominator;

    [resultFraction setTo: resultNum over: resultDenom];
    [resultFraction reduce];

    return resultFraction;
}

Let me explain what I think is going on.

For the first iteration of the loop, sum is allocated, then I enter the add: method, and the resultFraction is allocated. It is returned to sum2, meaning that resultFraction does not take any memory after being returned.

The first sum, allocated before the loop, is released and sum = sum2, meaning that the object “in” sum2 is now “in” sum, and sum2 does not take any memory after the assignment. Next, a new resultFraction is allocated and returned to sum2, which is free until now, and so on, until sum is released after exiting the loop.

Now there is just one object (the one returned by add:), and it is just being assigned to sum / sum2 (pointers? or ?). However, it is not like I thought it was — that when sum2 is being assigned a new object (the one returned by add:), and not being released, even after assigning that object to sum, the previous one is still there. This means that after n assignments there will be n objects “in” sum2. and because of that when I try to release both sum and sum2 before pool drain I get the error. The error is from the second object I try to release, and I just can release either sum or sum2, because they both are connected to the last object returned by the add: method?

I hope I was clear enough, because I’ve been banging my head against the wall all day long, and it just came to me and I really hope I got this right so I can continue with the book. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T01:56:41+00:00Added an answer on May 21, 2026 at 1:56 am

    What book is that? That is some distinctly non-standard memory management patterns.

    First, this method:

    - (Fraction *) add: (Fraction *) f
    

    Should be returning an auto-released object. It currently isn’t. This leads to mass confusion on the calling side in that the mantra is no longer “if you want to keep an object return value (beyond NARC), you must retain it”.

    Next, when you see an expression like sum = sum2; (where both variables are object references; Foo*), that is exactly like the expression x = 5;. It is a simple numerical assignment; no retain/release implied.

    Thu, if you have:

    sum = [[Fraction alloc] init];
    sum2 = [[Fraction alloc] init];
    sum = sum2;
    

    You’ve just leaked the Fraction instance that sum2 was referring to. So:

    • think of retains/releases as deltas; you increase or decrease the count. As long as your increases are exactly balanced with your decreases, you are doing it right.

    • think of the sum in Fraction *sum; as a potential reference to an object. When declared, it is nothing. When you assign it to the result of [[Fraction alloc] init];, there is no magic — sum just holds the address of the Fraction object in memory.


    Are you referring to this?

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

    The release releases the old sum before overwriting that pointer with a reference to a new object on the next line.

    Try build and analyze on that code. It will produce warnings. The book is teaching you how to do memory management using a pattern that doesn’t leak, but is decidedly not standard. I’m not convinced that going down that path is useful; the reality is that you will always have autorelease in play and, thus, should always follow the standards of the system, even in your own totally isolated code. Why spend the time learning, then un-learning, a different pattern at this point? (I’m all for learning different patterns and systems… just not in this context).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently started learning F#, and this is the first time I've ever
I jut recently started learning PHP from a book called PHP/MySQL Programming for the
I started learning some Android development with a beginner's book I bought recently. The
I recently started learning Emacs . I went through the tutorial, read some introductory
I recently started learning Python and I was rather surprised to find a 1000
I have recently started learning Perl and one of my latest assignments involves searching
I've been a web developer for some time now, and have recently started learning
My company has recently started using Scrum; we've done 2 sprints. We're still learning,
Having only recently started learning the 'new' OpenGL (programmable as opposed to fixed-function, I
I started learning Django recently, and can't find answer for a simple question. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.