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

  • Home
  • SEARCH
  • 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 4249494
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:24:16+00:00 2026-05-21T04:24:16+00:00

We have a class that wraps NSURLConnection. It accepts a block that it calls

  • 0

We have a class that wraps NSURLConnection. It accepts a block that it calls back when it finishes loading. To give you an idea, see below. When you send a request, it saves the callback on the instance. Assume my class is named Request

// from Request.h
@property (nonatomic, copy) void(^callback)(Request*);
- (void) sendWithCallback:(void(^)(Request*))callback;

My code to use one looks something like this:

Request * request = [Request requestWithURL:url];
[request sendWithCallback:^(Request * request) {
    // do some stuff
}]

My question is: what does the block do to the retain count of request? Does it copy/retain it? Notice that I didn’t put __block in front of the definition.

I just changed something major in Request (switched from a synchronous NSURLConnection to async ASIHTTPRequest), and it started deallocing almost immediately after sending (causing delegate methods to call a dealloced object). With the sync NSURLConnection, that never happened.

I guess it makes sense that it would get dealloced with async, but how would I retain request appropriately? If I retained it right after I created it, I’d have to release it in the callback, but the callback doesn’t get called if the request is cancelled, and would create a memory leak.

  • 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-21T04:24:17+00:00Added an answer on May 21, 2026 at 4:24 am

    what does the block do to the retain count of request? Does it copy/retain it?

    No, it doesn’t.

    Request * request = [Request requestWithURL:url];
    [request sendWithCallback:^(Request * request) {
        // The request argument shadows the request local variable,
        // this block doesn't retain the request instance.
    }]
    

    If the block doesn’t have the request argument,

    Request * request = [Request requestWithURL:url];
    [request sendWithCallback:^{
        // If you use the request local variable in this block,
        // this block automatically retains the request instance.
    }]
    

    In this case, it would cause retain cycles (the request retains the block, the block retains the request).

    • AsyncURLConnection.h
    • AsyncURLConnection.m

    Please take a look at my AsyncURLConnection class. NSURLConnection retains AsyncURLConnection instance, so you don’t own AsyncURLConnection stuff by yourself.

    How to use

    [AsyncURLConnection request:url completeBlock:^(NSData *data) {
        // Do success stuff
    } errorBlock:^(NSError *error) {
        // Do error stuff
    }];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Java, say you have a class that wraps an ArrayList (or any collection)
I have a class called DatabaseHelper that wraps a DbConnection. What's the proper way
I have a class that wraps the GetGlobalResourceObject and GetLocalResourceObjet so they can be
I have a class that wraps List<> I have GetValue by index method: public
Say I have a class that wraps the Controller class: public class MyController :
I'm working on an asp.net MVC application. I have a class that wraps a
I have a repository class that wraps my LINQ to SQL Data Context. The
I have a static class that wraps some native methods from winspool: public static
This may be familiar to some. I have a wrapper class Ex that wraps
I have an Adapter class that wraps an object called X_Session. This adpater expects

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.