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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:38:06+00:00 2026-05-25T06:38:06+00:00

I have been learning Objective C on my own for some time already and

  • 0

I have been learning Objective C on my own for some time already and still don’t quite get the hang of memory management. When should I release properties?

Example, I have a class that will handle 2 (register & updateParticulars) different URLRequest connections. updateParticularsConnection will be performed when registerConnection finishes.

@interface ConnectionViewController : UIViewController {

}
@property (nonatomic, retain) NSURLConnection *registerConnection;
@property (nonatomic, retain) NSURLConnection *updateParticularsConnection;
@property (nonatomic, retain) NSMutableData *responseData;
@property (nonatomic, retain) NSMutableURLRequest *requestURL;

@end

@implementation ConnectionViewController
@synthesize registerConnection, updateParticularsConnection, responseData, requestURL,


(void)performRegistration {
    // other here to prepare the data.
    requestURL = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"myURL"]];
    registerConnection = [[NSURLConnection alloc] initWithRequest:requestURL delegate:self startImmediately:YES];
}

(void)updateParticulars {
    // other here to prepare the data.
     [requestURL setURL:[NSURL URLWithString:@"http:myURL.com"]];
     updateParticularsConnection = [[NSURLConnection alloc] initWithRequest:requestURL delegate:self startImmediately:YES];
}

Handling delegate callbacks

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [SVProgressHUD dismissWithError:@"Unable to connect"];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    if (responseData == nil) {
        responseData = [[NSMutableData alloc] init];
    }
    [responseData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    if (connection == registerConnection) {
        NSMutableString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        NSLog(@"Register connection recieved data reads : %@", responseString);
        if ([responseString isEqualToString:@"-1"]) { // error. stop connection
            [self.requestURL release]; // remember to release requestURL since we would not be continuing on.
        }   
        else if ([responseString isEqualToString:@""]) { // error. stop connection

            [self.requestURL release]; //remember to release requestURL since we would not be continuing on.
        }

        else {
            [self updateParticulars]; // perform next connection, updateParticulars

        }       
        responseData = nil; // clear the current stored data in preparation for the next connection.
        [self.registerConnection release];
        [responseString release];
    } // end of definition for register connection

    else if (connection == updateParticularsConnection) {
            // do stuff with data received back here 
        self.responseData = nil;
        [self.requestURL release];
        [self.updateParticularsConnection release];
    }       
}

My question is should I be releasing my properties as soon as I can, which is what I think I am doing now? Or only during the dealloc method? Do advise if I am not doing it right.

  • 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-25T06:38:07+00:00Added an answer on May 25, 2026 at 6:38 am

    you sort of need to take it on a case by case basis. the general answer is “as soon as you are done with it”, unless it is a trivial allocation. for trivial allocations (e.g. NSString * firstName), you can just wait until dealloc or it is replaced (e.g. setFirstName:). this merely simplifies the implementation.

    your example is slightly different.

    // typically, you will set these to nil right when they 
    // have finished and you have grabbed what you need.
    // that's pretty common for an async one shot request.
    @property (nonatomic, retain) NSURLConnection *registerConnection;
    @property (nonatomic, retain) NSURLConnection *updateParticularsConnection;
    @property (nonatomic, retain) NSMutableURLRequest *requestURL;
    

    and

    // in the context of your program, this allocation could be large.
    // in many cases, you'll simply convert it to the destination (if
    // it's an image, just turn it into an image without holding onto
    // the data representation), then dispose of it.
    @property (nonatomic, retain) NSMutableData *responseData;
    

    important: you are handling your instance’s ivars directly in the OP – use the accessors, they will save you a ton of headaches. these are the changes you’d make to write your program using the accessors:

    - (void)performRegistration {
        self.requestURL = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"myURL"]] autorelease]; // use setter and autorelease
        self.registerConnection = [[[NSURLConnection alloc] initWithRequest:requestURL delegate:self startImmediately:YES] autorelease]; // use setter and autorelease
    }
    
    - (void)updateParticulars {
        [self.requestURL setURL:[NSURL URLWithString:@"http:myURL.com"]]; // use getter
        self.updateParticularsConnection = [[[NSURLConnection alloc] initWithRequest:requestURL delegate:self startImmediately:YES] autorelease]; // use setter and autorelease
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        if (self.responseData == nil) { // use getter
            self.responseData = [NSMutableData data]; // use setter and autorelease
        }
        [self.responseData appendData:data]; // use getter
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        if (connection == self.registerConnection) { // use getter
            NSMutableString *responseString = [[NSString alloc] initWithData:self.responseData encoding:NSUTF8StringEncoding]; // use getter
            NSLog(@"Register connection recieved data reads : %@", self.responseString); // use getter
            if ([responseString isEqualToString:@"-1"]) {
                self.requestURL = nil; // use setter
            }
            else if ([responseString isEqualToString:@""]) {
                self.requestURL = nil; // use setter
            }
            else {
                [self updateParticulars];
            }
            self.responseData = nil; // use setter
            self.registerConnection = nil; // use setter
            [responseString release];
        }
        else if (connection == self.updateParticularsConnection) { // use getter
            self.responseData = nil; // use setter
            self.requestURL = nil; // use setter
            self.updateParticularsConnection = nil; // use setter
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been learning python for some time now. While starting this learning python
I have been learning about various functional languages for some time now including Haskell,
I have been learning C++ for three months now and in that time created
I have been learning C++ with some books from school that are from the
I have been learning some of the more detailed loopholes of JS here and
I have been learning Python by following some pygame tutorials . Therein I found
I have been learning the basics of generics in .NET. However, I don't see
I have been learning Objective-C as my first language and understand Classes, Objects, instances,
I have been learning PHP/Mysql for a while. But don't yet know enough to
Since I started learning Objective-C and Cocoa, I've been wondering why they have chosen

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.