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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:39:32+00:00 2026-06-07T12:39:32+00:00

I am having problem with understanding one concept of memory managment, because I am

  • 0

I am having problem with understanding one concept of memory managment, because I am new to objective C. For instance lets say I have a class Bar and Foo.

in main function I call:

Foo *foo = [bar getFoo]; //In my bar method I return foo
[foo retain];
[foo callMethod];
[foo release];

I know this is right way to do it. But why do we have to retain it after we get it from another object, does not this mean returning object has retain count 0 ? so we have to reatin it to count 1 to use it? but if it has reatin count 0, how do we know it is still there. We can assume since it is the next line that increment retain count that the object memory wont be realocated, but what if we have multi-threading program?

  • 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-07T12:39:35+00:00Added an answer on June 7, 2026 at 12:39 pm

    When an class method returns an object, it will autorelease it so you don’t have to bother; typically:

    - (Foo *)getFoo
    {
        return [[_foo retain] autorelease];
    }
    

    If you are only using foo for the lifetime of the calling method you don’t need to retain it, as it won’t be autoreleased until next time through the run loop, so your code should actually be:

    Foo *foo = [bar getFoo]; //In my bar method I return foo
    [foo callMethod];
    

    If, however, you want to hold foo for a while, outside the scope of the calling method, you need to retain it and then release it sometime later.

    One more thing; the convention for getter method names is simply “name”, so your setter should be setFoo and your getter would be foo. Keeping to the naming conventions is a good idea as it lets you know what a method does, in say 7 months time, and tools like static analysis understand the conventions.

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

Sidebar

Related Questions

i am studying apple obj-c guide and i am having problem understanding class types,
i'm new to c++ and having a little problem understanding about c++'s casting. According
I'm quite new to OpenId and I'm having a bit of a problem understanding
I am relatively new to C++ and am having problems understanding struct. I have
I'm having trouble passing a variable defined in one class instance to another class
I'm having a problem understanding how to do something in LINQ. I have a
I am having a problem understanding how array.sort{ |x,y| block } works exactly, hence
I am having a problem understanding how polymorphism works when using generics. As an
I've been having some difficulty in understanding the source of a problem. Below is
I'm having problems understanding how latest.integration works. I have an example that is not

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.