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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:05:19+00:00 2026-05-16T00:05:19+00:00

Completely new to Objective-C, trying to find out when I need to alloc and

  • 0

Completely new to Objective-C, trying to find out when I need to alloc and release objects.

For example, I want to fetch some data from the Web. I found an article at Apple which has this code:

NSURLRequest *theRequest=[NSURLRequest requestWithURL:
                  [NSURL URLWithString:@"http://www.apple.com/"]
                    cachePolicy:NSURLRequestUseProtocolCachePolicy
                timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] 
                                 initWithRequest:theRequest delegate:self];

What I don’t understand is: Why do they need to call alloc on the connection but not on the request? How do I know when I need alloc and when not?

Similar questions for release. From what I read, I only ever need to release objects that were initialized using alloc/init. But all “initWithXXX” functions return autoreleased objects instead.

Is this a hard rule, or just convention? Is there a way to find out if I need to release an object or not?

  • 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-16T00:05:20+00:00Added an answer on May 16, 2026 at 12:05 am

    Please read the Memory Management Programming Guide. All of these are explained there. Also check out Learn Objective-C.


    Why do they need to call alloc on the connection but not on the request?

    Whenever you need to own an object to make it live longer than the function’s scope, the object needs to be -retained. Of course you could use

    NSURLConnection* theConnection = [NSURLConnection connectionWithRequest:theRequest
                                                                   delegate:self];
    

    but the connection will be -autoreleased. But because there’s time for a connection to finish, this variable should be -retained to prevent the connection becoming invalid. Of course, then, you could do

    NSURLConnection* theConnection = [[NSURLConnection connectionWithRequest:theRequest
                                                                   delegate:self] retain];
    

    But this is equivalent to +alloc → -init → -autorelease → -retain, the last two steps are redundant. Probably that’s why Apple chooses to use +alloc/-init here.

    (BTW, this style would cause the static analyzer to complain. It’s better to store theConnection as an ivar somewhere, e.g. in the delegate object.)

    On the other hand, the NSURLRequest is just a temporary object, so it needs to be -released when the function ends. Again, you could use

    NSURLRequest* theRequest = [[NSURLRequest alloc] initWithURL:...];
    ...
    [theRequest release];
    

    this is even more efficient, as the autorelease pool won’t be filled up, but using this method one may forget to -release and cause a leak.

    But all “initWithXXX” functions return autoreleased objects instead.

    No, -init… should never return an -autoreleased object.

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

Sidebar

Related Questions

I need some clarification/advice regarding modifying objects stored in mutable containers in Objective-C. Let's
I am completely new to objective c. I am trying to compile a very
I am Completely new to Objective C and Cocoa programming. Why does NSApplicationMain not
I am completely new to Jena/TDB. All I want to do is to load
I'm completely new to javascript, but I'm trying to create a beta form for
I'm completely new with IFC files. For my company I need to read IFC
This is probably a completely stupid question, but i'm pretty new at objective-C and
I'm trying to implement the Strategy pattern using Core Data and Objective C. To
I am completely new to objective-c and I am currently in the learning process,
Completely new to most of this stuff, but basically Im playing around with the

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.