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

  • SEARCH
  • Home
  • 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 7996607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:45:02+00:00 2026-06-04T14:45:02+00:00

I have a strange (looks to me) and simple memory leak situation using ARC

  • 0

I have a strange (looks to me) and simple memory leak situation using ARC (automatic reference counting). I am working with iOS code, but I think it should apply to Objective-C in general.

I have a following function that returns the object assigned as a parameter after checking the properties of the parameter object.

- (id) returnParam:(id) obj
{
    // Do whatever filtering needed. Return nil for invalid input.

    return (NSNumber *)obj;
}

If I call this method in a loop as follows, I can see that the allocated memory keeps increasing until the loop ends in Instruments.

for(int i = 0; i < 1000000; i++)
{        
    id obj = [[NSNumber alloc] initWithInt:i];        
    id obj2 = [self returnParam:obj];
    NSLog(@"attempt %@", obj2);
}

However, if I put the content of the returnParam function in the loop as follows, everything works fine. The memory foot stamp stays the same size all long.

for(int i = 0; i < 1000000; i++)
{        
    id obj = [[NSNumber alloc] initWithInt:i];

    // Do whatever filtering needed. Break on invalid input.

    id obj2 = obj;

    NSLog(@"attempt %@", obj2);
}

I have eliminated the filtering part (so essentially the function only passes the object back to the caller), and the same situation persists.

Not understanding why this sequence does not decrease retain count as supposed, I tried all possible combination of __weak, __unsafe_unretained here and there, but none worked.

Can somebody explain why this (returning parameter object) doesn’t work, and suggest me a solution of this issue?

P.S. BTW, it is not captured as a memory leak event in Instruments, but the situation as I consider is a obvious memory leak.

  • 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-06-04T14:45:03+00:00Added an answer on June 4, 2026 at 2:45 pm

    It’s not a leak. It has to do with how -autorelease works. Expanding on what -returnParam: does:

    -(id) returnParam:(id) paramToReturn
    {
       // make sure the variable is in the scope for the entirety of the function
       id returnValue = nil;
       @try 
       {
           // when the parameter is passed in, ARC will automatically retain it.
           [paramToReturn retain];
           returnValue = [paramToReturn retain]; // strong assignment (a = b)
           return returnValue;
       }
       @finally
       {
           [paramToReturn release]; // release the value we retained when we passed in the parameter
           [returnValue autorelease]; // autorelease the return value so it is still valid after the function exits.
       }
    }
    

    Now, let’s compare this to your other loop:

    for (int i = 0; i < 1000; i++)
    {
        id obj = [[NSNumber numberWithInt:i] retain]; // retain, because 'obj' is strong by default.
        id obj2 = [obj retain]; // retain, because 'obj2' is strong by default
    
        NSLog(@"attempt %@", obj2);
    
        [obj release]; // release because 'obj' is strong
        [obj2 release]; // release because 'obj2' is strong.
    }
    

    Thus, your variables aren’t getting cleaned up until the next autorelasepool is popped, which usually occurs at the next NSRunLoop tick in an iPhone app, or possibly at the end of the @autoreleasepool in a console application.

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

Sidebar

Related Questions

This code compiles but looks very strange. I have a typical and simple parent/child
I have a quite strange situation. I have this very simple package: Task get
I have strange problem with receiving data from socket. On client im using air
I'm working on a quite simple native extension for Adobe Air powered iOS app.
I have very strange problem with mySQL and simple query with simple index. I
I have a really simple class, and within the implementation I have this code:
I have a simple Google maps web application I'm working on. I have purchased
I'm having a strange behavior in Linq to Sql... I have a simple parent
I have very strange problem. I'm using IIS 7.0 Integrated mode for my application.
I have some strange problem. I think I followed documentation correctly but my code

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.