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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:24:14+00:00 2026-06-10T03:24:14+00:00

I understand that when using Alloc, new or copy you own the object and

  • 0

I understand that when using Alloc, new or copy you own the object and need to release the object. I understand that if I retain an object that I need to release it.

But if I have the following statment at the end of a method:

return [[UIImage alloc] initWithContentsOfFile:path];

I own the UIImage object becaused I allocated the memory space, but I dont have a reference to it anymore because it was returned to the caller. In my dealloc() method I can’t release it, since I dont have a reference.

So my question is, is this correct:

return [[[UIImage alloc] initWithContentsOfFile:path] autorelease];

I beleive in this case the caller then can retain the returned object if they like to take ownership and will eventually need to release themselves.

Thanks for your help.

  • 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-10T03:24:16+00:00Added an answer on June 10, 2026 at 3:24 am

    The whole point of autorelease was built around returning objects.

    - (id)bad {
        MyObj *obj = [[MyObj alloc] init];
        return obj;
    }
    

    This code returns everything correctly, but you (as developer) must be sure to release the object later on.

    - (id)moreBad {
        MyObj *obj = [[MyObj alloc] init];
        return [obj release];
    }
    

    This code uses the memory as expected, balancing retain and release in one scope, but returned object is garbage now (expect this to crash).

    - (id)good {
        MyObj *obj = [[MyObj alloc] init];
        return [obj autorelease];
    }
    

    This code is “delayed release”. I.e. you can retain the object at the caller side and the object would be safe. It could surely be broken if you don’t have NSAutoreleasePool in scope, but that’s a pretty rare condition, and you mostly know when that happens (the most common scenario is to start a new thread where there is no “default” autorelease pool).

    So, the common practice is to balance the retain (and retain-like) and release methods in one scope. Next suggestion is to always have objects alloc-init-autoreleased, and switch to distinct releases if you have memory issues (like autoreleasing lots of objects in a loop). Next suggestion is to switch to ARC.

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

Sidebar

Related Questions

I understand that using the facebook API I need an api key to connect,
Ok, I understand that using strings that have special characters is an encoding issue.
I understand that this can be done at release using the maven-gpg-plugin. However, I
I have an iframe Facebook application (using Facebook's new JavaScript API) that needs to
I understand that using postgres on osx is a little difficult because it has
I understand that time(0) is commonly using for seeding random number generators and that
I understand that I can change a sql table using the follow sp: EXEC
I understand that we can set the various style attributes from code behind using
I understand that it is good practice to use a using block when getting
*edit: I understand that stackoverflow has banned all questions regarding using Microsoft's Visio. I'm

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.