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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:14:33+00:00 2026-06-12T17:14:33+00:00

I am confused in some condition when I have to release an object? So

  • 0

I am confused in some condition when I have to release an object? So I wants to know when we release objects in objective C. Can I use autorelease where I alloc the objects any disadvantage of autorelease? Where release the following objects?

Case 1:

SelectFrame *obj=[[SelectFrame alloc]initWithNibName:@"SelectFrame" bundle:nil];
[[self navigationController] pushViewController:obj animated:YES];

Case 2:

UIView *barView=[[UIView alloc]initWithFrame:CGRectMake(0, 500, 200,50)];
barView.backgroundColor=[UIColor redColor];
[self.view addSubview:barView];

Case 3:

NSURLRequest *request = [NSURLRequest requestWithURL:
                         [NSURL URLWithString:@"https://xxxxxxxxxxxxxxxxx"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
  • 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-12T17:14:34+00:00Added an answer on June 12, 2026 at 5:14 pm

    Yes, you have to release for the Above two cases.

    Case 1:

    SelectFrame *obj=[[SelectFrame alloc]initWithNibName:@"SelectFrame" bundle:nil];
    [[self navigationController] pushViewController:obj animated:YES];
    [obj release];
    

    Case 2:

    UIView *barView=[[UIView alloc]initWithFrame:CGRectMake(0, 500, 200,50)];
    barView.backgroundColor=[UIColor redColor];
    [self.view addSubview:barView];
    [barView release]; 
    

    Case 3:

    NSURLRequest *request = [NSURLRequest requestWithURL:
                         [NSURL URLWithString:@"https://xxxxxxxxxxxxxxxxx"]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
    

    You don’t need a release here, as the request object is in autoreleased mode.

    Remember two things.

    1.) You have to manually release an object when you retain or alloc-init that object.

    2.) Class methods which do not have alloc methods return an autoreleased object hence you don’t need to release those objects.

    Disadvantage of using autorelease :

    Ok, so what does autorelease mean? Autorelease means , not us, but our App would decide when to release the object. Supposing case 2 of your question. After barView is added to self.view there is no need of this allocated object. Hence, we release it. But, if we had kept it in autorelease mode, the App would keep it for longer time, wasting some part of memory by still keeping that object. Hence, we shouldn’t use autorelease here.

    Advantage of using autorelease :

    This over-popular example.

    - (NSString*) getText
    {
        NSString* myText = [[NSString alloc] initWithFormat:@"Hello"];
        return myText;
    }
    

    Here, the 3rd line causes a leak because we do not release the memory allocated to myText.
    Hence,

    - (NSString*) getText
    {
        NSString* myText = [[[NSString alloc] initWithFormat:@"Hello"] autorelease];
        return myText;
    }
    

    SOLUTION

    Use ARC, forget retain release 🙂

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

Sidebar

Related Questions

I got confused with Git !! I have some files which have added and
I'm totally confused on this matter, some say use tables and others say it's
I'm still confused... When we write some thing like this: Object o = new
I'm looking through my textbook and I'm a little confused about some of the
This might be basic question, however I am confused on some .Net Concpets. I
For some reason this function confused me: def protocol(port): return port == 443 and
I'm trying to do some simple things in C and I'm confused. The program
Some of the HTML5 canvas demos are very impressive, but I'm a bit confused.
I'm trying to do some research around file systems and I've probably confused myself.
I confused with extends, i went with theory n solved some examples, But when

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.