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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:14:43+00:00 2026-05-23T20:14:43+00:00

I have a connection class that uses NSURLConnection to connect to the server. While

  • 0

I have a connection class that uses NSURLConnection to connect to the server. While in main class I call a class method of this class, the class method then allocates instance of itself and when the delegate ConnectionDidFinish is received, I release same class from within. Is this approach correct or this will lead to some problem.

Main Class :

[ConnectionClass connectToServer];

Connection Class :

@implementation ConnectionClass
 +(void)connectToServer{
   connectionClass = [[ConnectionClass alloc] init];
   [connectionClass createConnection];
 }
 -(void)createConnection{
    NSURLConnection *connection = [[NSURLConnection alloc] initWithDelegate:self];
  // create asynchronous connection
 }
 - (void)connectionDidFinishLoading:(NSURLConnection *)connection{
   [self release];
 } 
@end

Is it good to release self within its own method ?

What if I do it something this way;

Main Class :

[connectionClass setDelegate:self];
[connectionClass connectToServer];

Connection Class :

@implementation ConnectionClass
 -(void)connectToServer{
  [connectionClass createConnection];
 }
 -(void)createConnection{
  NSURLConnection *connection = [[NSURLConnection alloc] initWithDelegate:self];
  // create asynchronous connection
 }
 -(void)connectionDidFinishLoading:(NSURLConnection *)connection{
  [self.delegate finishedConnection:self]; // added delegate and then called to the main class and pass the self object for main to release it
 } 
 @end

And in the main class delegate we, release the object,

-(void)finishedConnection:(ConnectionClass*)connection
{
  [connection release];
 }

IS there any problem in releasing the object this way ?

  • 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-23T20:14:44+00:00Added an answer on May 23, 2026 at 8:14 pm

    I would do this:

    @implementation ConnectionClass
    
    + (void)connectToServer {
    
        connectionClass = [[ConnectionClass alloc] init];
        [connectionClass createConnection];
        [connectionClass release];
    }
    
    - (void)createConnection {
    
        [self retain];
    
        NSURLConnection *connection = [[NSURLConnection alloc] initWithDelegate:self];
        // create asynchronous connection
        [connection release];
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        [self release];
    }
    
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
        [self release];
    }
    
    @end
    

    That way the ConnectionClass object is self retaining, and you’re not putting the retain/release responsibility in different places of code, that are not tightly related.

    Edit: As Rabskatran points out, if you’re just learning about retain/release, then this is not the optimal solution.

    Your second example with the delegate is better. I’d let the connectionClass object be an instance variable, so you can message the connection object to cancel the operation when the main class (which would be the connection’s delegate) gets deallocated.

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

Sidebar

Related Questions

I have this class that contains vars for db connection; Imports Microsoft.VisualBasic Imports System.Data.SqlClient
I have a server that listens for a connection on a socket: public class
I have a tcp server that uses TCPListener and the async method BeginAcceptTCPClient: Imports
I have a Service that uses a custom Connection class (extends thread) to a
I have a utility class that creates & returns a db connection: Public Shared
I have a class that contains a collection. I want to provided a method
I'm using Spring 3.0.2 and I have a class called MovieDAO that uses JDBC
I have a class that uses libxml2. It has static members which are used
I have a console app that uses 20 or so threads to connect to
I have an ActiveRecord Model, PricePackage. That has a before_create call back. This call

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.