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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:43:20+00:00 2026-06-15T18:43:20+00:00

I have two rookie questions about memory management in non-ARC project without GC: in

  • 0

I have two rookie questions about memory management in non-ARC project without GC:
in Objective C,when I ‘autorelease’ object and return it from method, should I ‘release’ it in the “parent method” calling the method retuning the object?

The second question is, that I returned object (f.e. user) from method (instance method on another object, f.e. userService) and saved it to property (self.userProp, self instantiated the object userService and called his method). When I released the object userService (whose method returned the object user, stored in property self.userProp), it released also the object stored in property (object user stored in property self.userProp). Why? Who was the owner of the object in property?

Thank you

  • 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-15T18:43:21+00:00Added an answer on June 15, 2026 at 6:43 pm

    when I ‘autorelease’ object and return it from method, should I
    ‘release’ it in the “parent method” calling the method retuning the
    object?

    No. Here’s why. Let’s layout an example exactly as you’ve said:

    -(NSObject*)someMethodThatReturnsAnAutoreleasedObject {
        return [[[NSObject alloc]init]autorelease];
    }
    
    -(void)myMethod {
        NSObject *obj = [self someMethodThatReturnsAnAutoreleasedObject];
        [obj doSomething];
        [obj release] //PROBLEM!
    }
    

    Look at the retain count of the objects step by step, and you’ll see that while the -autorelease may not immediately cause an issue for you, it will at some later date (because -autorelease‘d objects aren’t “auto-released”, rather they are deallocated en masse when their owning pool is drained or deallocated). The method that returns an autoreleased object returns it with an eventual retain count of 0 (0 (start) + 1 (alloc) – 1 (autorelease)), so you releasing it is not only unnecessary, but will cause a crash.

    The second question is, that I returned object (f.e. user) from method
    (instance method on another object, f.e. userService) and saved it to
    property (self.userProp, self instantiated the object userService and
    called his method). When I released the object userService (whose
    method returned the object user, stored in property self.userProp), it
    released also the object stored in property (object user stored in
    property self.userProp). Why? Who was the owner of the object in
    property?

    Again, a little setup first (this is rather convoluted at first, but makes complete sense afte you see what you’re doing).

    -(NSUserService*)someMethodThatReturnsAnAutoreleasedObject {
        return [[[NSUserService alloc]init]autorelease];
    }
    
    -(void)myMethod {
        self.userservice = [self someMethodThatReturnsAnAutoreleasedObject];
        [obj doSomething];
        [obj release] //No problem, assuming userService is declared retain or strong.
    }
    

    Well, why is this different? Memory qualifiers, that’s why! When you declare a property, you declare a set of memory qualifiers to tell the compiler what kind of modifier to append to the assignment sign. Most properties are declared retain or strong ((under ARC), and hopefully your userService one is declared as such, as well) if they are object types, which makes the compiler interpret

    self.userservice = [self someMethodThatReturnsAnAutoreleasedObject];
    

    as

    self.userservice = [[self someMethodThatReturnsAnAutoreleasedObject]retain];
    

    Therefore, you must release it, or you have a leak on your hands (0 (start) + 1 (alloc) – 1 (autorelease) + 1 (retain) = +1).

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

Sidebar

Related Questions

I have two dumb/rookie questions. My problem is, I have NO idea where to
I have two tables one with an add button and one without an add
I have two columns, the first column will have the name of a object,
Have two folders with approx. 150 java property files. In a shell script, how
Have two actionsheet buttons and one modalviewcontroller on mainviewcontroller in application. Now for two
I have two tables images2 and image_data So the goal is to have 1
I have two classes (MVC view model) which inherits from one abstract base class.
I have two files client.php and server.php. The client file send a HTTP request
I have two columns say Main and Sub . (they can be of same
I have two tables 1.Products prod_id prod_name 1 honda 2 hero 3 marcedes 4

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.