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 7848319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:09:39+00:00 2026-06-02T18:09:39+00:00

When compiling with ARC, method arguments often appear to be retained at the beginning

  • 0

When compiling with ARC, method arguments often appear to be retained at the beginning of the method and released at the end. This retain/release pair seems superfluous, and contradicts the idea that ARC “produces the code you would have written anyway”. Nobody in those dark, pre-ARC days performed an extra retain/release on all method arguments just to be on the safe side, did they?

Consider:

@interface Test : NSObject
@end

@implementation Test

- (void)testARC:(NSString *)s
{
  [s length];  // no extra retain/release here.
}

- (void)testARC2:(NSString *)s
{
  // ARC inserts [s retain]
  [s length];
  [s length];
  // ARC inserts [s release]
}

- (void)testARC3:(__unsafe_unretained NSString *)s
{
  // no retain -- we used __unsafe_unretained
  [s length];
  [s length];
  // no release -- we used __unsafe_unretained
}

@end

When compiled with Xcode 4.3.2 in release mode, the assembly (such that I’m able to understand it) contained calls to objc_retain and objc_release at the start and end of the second method. What’s going on?

This is not a huge problem, but this extra retain/release traffic does show up when using Instruments to profile performance-sensitive code. It seems you can decorate method arguments with __unsafe_unretained to avoid this extra retain/release, as I’ve done in the third example, but doing so feels quite disgusting.

  • 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-02T18:09:40+00:00Added an answer on June 2, 2026 at 6:09 pm

    See this reply from the Objc-language mailing list:

    When the compiler doesn’t know anything about the
    memory management behavior of a function or method (and this happens a
    lot), then the compiler must assume:

    1) That the function or method might completely rearrange or replace
    the entire object graph of the application (it probably won’t, but it
    could). 2) That the caller might be manual reference counted code, and
    therefore the lifetime of passed in parameters is not realistically
    knowable.

    Given #1 and #2; and given that ARC must never allow an object to be
    prematurely deallocated, then these two assumptions force the compiler
    to retain passed in objects more often than not.

    I think that the main problem is that your method’s body might lead to the arguments being released, so that ARC has to act defensively and retain them:

    - (void) processItems
    {
        [self setItems:[NSArray arrayWithObject:[NSNumber numberWithInt:0]]];
        [self doSomethingSillyWith:[items lastObject]];
    }
    
    - (void) doSomethingSillyWith: (id) foo
    {
        [self setItems:nil];
        NSLog(@"%@", foo); // if ARC did not retain foo, you could be in trouble
    }
    

    That might also be the reason that you don’t see the extra retain when there’s just a single call in your method.

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

Sidebar

Related Questions

I'm accessing the address book and have this line and am compiling using ARC:
Compiling this code with g++ 4.7.0 ( -Wall -Wextra -Werror -Wconversion -std=c++11 ): #include
While compiling a program in Java I got this big WARNING warning: [unchecked] unchecked
I have the following code and am getting this error before compiling: Fast Enumeration
Compiling on linux using gcc. I would like to convert this to hex. 10
Compiling this code int main(int argc, char **argv) { int xor = 0; }
Compiling this code using g++ 4.2.1: struct S { }; template<typename T> struct ST
This is my simple piece of code on iOS, using ARC: @interface Person :
Compiling this example #include <boost/bind.hpp> #include <boost/algorithm/string.hpp> #include <algorithm> #include <iostream> #include <vector> using
Compiling my iPad app against the 5.1 SDK (release version) causes UIPopoverController to show

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.