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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:29:59+00:00 2026-05-12T18:29:59+00:00

Everything i’ve read about objective-c memory management makes it sound incredible easy. To paraphrase

  • 0

Everything i’ve read about objective-c memory management makes it sound incredible easy. To paraphrase everyone: “release whatever you alloc, retain or copy”. But I think there are some more specific cases that are not so clear cut. Below are some sample situations. What is the correct procedure for each:

SITUATION #1:

Foo *foo = [[foo alloc] init];
Foo *fooToo = [[[foo alloc] init] autorelease];

foo = fooToo;

does foo need to be released before it is assigned to fooToo?

SITUATION #2
I seem to get a lot of crashes when I do things like:
(.h and .m mashed together for convenience)

Foo *foo;
@property (nonatomic, retain) Foo *foo;
@synthesize foo;

-(id)init{
self = [super init];
}

-(void)dealloc{
[super dealloc];
[foo release];
}

I’m constantly told “always release in dealloc what you set a retain property for”. But this will crash, if done like I’ve shown.

SITUATION #3
A similar situation that will also crash:
(.h and .m mashed together for convenience)

Foo *foo;
@property (nonatomic, retain) Foo *foo;
@synthesize foo;

-(id)init{
self = [super init];
self.foo = nil;
}

-(void)dealloc{
[super dealloc];
[foo release];
}

for some reason when my code makes it down to dealloc, foo isn’t == nil.

SITUATION #4
Finally, just a question, what general thought process do people use when deciding between

 self.foo = bar;

and

 foo = bar;

when foo is declared the same as in the above cases? Is self.foo just another way of coding:

foo = [bar retain];

  • 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-12T18:29:59+00:00Added an answer on May 12, 2026 at 6:29 pm

    Situation #1:

    Yes, you do need to release foo before you lose your reference to it. Given that you allocated it, you are responsible for releasing it. If you re-assign foo before releasing it, you cannot release it anymore!

    Situation #2:

    The proper implementation of dealloc is:

    - (void)dealloc {
        [foo release];
        [super dealloc];
    }
    

    The dealloc method needs to call the super’s dealloc method not the release method. Also, you need release the fields first before calling [super dealloc]

    Situation #3:
    Same as situation #2.

    Situation #4:

    I always prefer using self.foo = bar, as it does the following steps automatically, if needed:

    1. releasing foo
    2. retaining bar
    3. assigning foo to bar

    The assignments foo = bar and foo = [bar retain] don’t release the previous object of foo.

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

Sidebar

Related Questions

Everything I read about better PHP coding practices keeps saying don't use require_once because
Everything I read online talks about Info.plist. XCode made me a [name of the
Everything I read about cookies says that setting the expiry time of a cookie
Everything I read about Hibernate states that you must roll back a transaction and
Everything I have read says that when making a managed stored procedure, to right
Everything is an object was one of the first things I learned about Ruby,
Everything i read tells me this should work page 1 is <?php $state =
Everything works fine, but only if file is small, about 1MB, when I tried
Everything I have read so far, it seems as though you copy the DB
Everything in nodejs is non-blocking which is nice, but how would I go about

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.