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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:32:32+00:00 2026-05-24T22:32:32+00:00

I use blocks to perform an access check in a service class, but it

  • 0

I use blocks to perform an access check in a service class, but it is leaking memory. Can someone point out what the problem with my method is? The problem is probably related to which variables I am using within the blocks. I am accessing instance variables, method variables and referencing super.

-(RequestDO*)requestWithURL:(NSString*)url andDelegate:(id<RequestDelegate>)delegate_ signURL:(BOOL)sign_ request:(RequestDO*)request_ postData:(NSString*) postData_ {

    if([self requiresUpdatedAccess]){

        if(accessRequest == nil){
            accessRequest = [[UpdatedAccessManager getPaymentStatus:self] retain];

            if(accessRequest.processStatus == kRequestComplete){ // Access check is complete (cached)
                [accessRequest release], accessRequest = nil;
                return [super requestWithURL:url andDelegate: delegate_ signURL:sign_ request:request_ postData: postData_];  // Do original service request
            }
            else{
                Block_release(completionBlock_);

                // When access check is done, we will perform this block to process the original request
                completionBlock_ = Block_copy(^(){
                    /*
                        url - an instance variable
                        delegate_, sign_, request_ and postData_ is method scoped variables
                    */                
                    NSString *updatedUrl = [Service updateUrlWithUserData: url];
                    [super requestWithURL:updatedUrl andDelegate: delegate_ signURL:sign_ request:request_ postData: postData_];
                });

                Block_release(failureBlock_);

                // If access check fails, we will perform this block to inform about the error
                failureBlock_ = Block_copy(^(RequestDO* req_, NSError* err_){
                    [delegate_ requestFailed:self.request withError: err_];
                });
            }
        }
    }
    else{
        return [super requestWithURL:url andDelegate: delegate_ signURL:sign_ request:request_ postData: postData_]; // This service does not need access check  
    }

    return accessRequest;
}

In dealloc i have Block_release for both blocks.

  • 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-24T22:32:33+00:00Added an answer on May 24, 2026 at 10:32 pm

    The block will retain all used vars. In your case the retain count of your object will be 2 due to the use of it in the block. Thus dealloc is not called and you have a classic retain cycle. To work around this declare

    __block id blockSelf = self;
    

    before the block and use blockSelf instead of self in your block.

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

Sidebar

Related Questions

Is there a way to use yield blocks to implement an IEnumerator<T> which can
I am trying to use Objective-C blocks in an iPhone 3.1.3 app but am
Can anyone explain why you can’t use inline code blocks within server control declarations
I can use vi{ and va{ to select C++ code blocks. It helps me
I have a class that can perform many types of transformations to a given
I am finding out how to use the 'From workspace' and 'to workspace' blocks
i use two blocks to draw some buttons on my subview dynamically. One calculates
Is it possible to use TRY CATCH blocks in SQL Selects? For stuff similar
so in my program I have parts where I use try catch blocks like
If you use NSEnumerationConcurrent while enumerating a collection using blocks, does Cocoa guarantee that

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.