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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:38:03+00:00 2026-05-16T17:38:03+00:00

I have a controller that makes HTTP GET requests using a custom class, which

  • 0

I have a controller that makes HTTP GET requests using a custom class, which acts as the delegate for the NSURLConnection. Once the NSURLConnection fails or finishes, the custom class invokes methods on the controller and passes along an NSData object of received data.

I’m running into a problem where the controller in action is being dynamically created and pushed onto a navigation controller’s stack. This controller makes the HTTP GET request in its viewDidLoad method. If the user quickly presses “back” on the navigation bar, this controller gets dealloc’ed. If this happens before the HTTP GET request finishes, the resulting NSURLConnection callback becomes a method call to a dealloc’ed object, which results in an EXC_BAD_ACCESS.

What’s the best approach to cleaning up any pending NSURLConnections that have been kicked off by a controller which may actually be deallocated already?

I threw in some NSLog statements, and it seems that my custom class used as a NSURLConnection delegate doesn’t actually receive a dealloc message. I made sure to set the controller’s instance of this class to nil in viewDidUnload, and also call release on it, but it still seems to live longer than the controller.

  • 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-16T17:38:03+00:00Added an answer on May 16, 2026 at 5:38 pm

    If I understand it correctly you just need to do [whateverConnection cancel] in your viewDidUnload or dealloc method. This cancels the connection. It is almost the same if you have a custom downloader object for example for some large image that uses NSURLConnection. Make a cancel method for your class (that cancels the connection and releases it) and invoke it in your controller’s dealloc method. You should also use a bool flag something like wasCanceled and do not invoke any method from your custom object’s delegate if wasCanceled was set from your cancel method. (You only have a weak pointer to your delegate so it is probably released already when some other object invokes your cancel method). I assume that the delegate for your custom object is the view controller. I had several downloaders like this and it worked ok, without leaks even if I quickly canceled a download.

    @interface CompaniesDownloader : NSObject /*<NSXMLParserDelegate>*/
    {
        id<CompaniesDownloaderDelegate> delegate; //a view controller is the delegate
        NSMutableArray *companies;
    
        BOOL isWorking;    
        BOOL wasCanceled;   
    
        @private
    
        //url connection object
        NSURLConnection *companiesConnection;
    
        //this is where i put the binary data that gets transformed into xml
        NSMutableData *webData;
    
        //temporary string used when parsing xml
        NSMutableString *tmpString;
    
        //temporary company used when parsing xml
        Company *tmpCompany;
    }
    

    In the implementation:

    -(void) cancel
    {
      [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible: FALSE];
    
      wasCanceled = TRUE;
      [companiesConnection cancel];
      [webData release];
      webData = nil;
      self.companiesConnection = nil; //OR [companiesConnection release]; companiesConnection=nil;
      isWorking = FALSE;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a controller that looks like this: public class PageController : Controller {
I have a controller that runs some calculation on data, and then I need
I have a controller that contains a tableView that spawns another controller modally to
I have a controller that is called with AJAX (sends JSON data), so I
--Summary (shortened)-- I have a controller that loads a profile object from the corresponding
When you have a controller that does logic with services and DAO's that may
I have a bit of a strange problem. I have a controller that is
I have a request controller that is getting out of hand, and I want
I have a simple controller that was created through a scaffold and has a
I have a view controller that houses a UIWebView. I want to display an

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.