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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:49:25+00:00 2026-06-09T09:49:25+00:00

Recently I was dropped into someone else’s codebase I’ve been able to tackle most

  • 0

Recently I was dropped into someone else’s codebase I’ve been able to tackle most things it’s thrown up so far but this one is a little over my head. There’s some retain cycles that I can’t figure out how to fix.

There is a custom object that wraps an FROAuthRequest, the FROAuthRequest has a completion block in which there are a further 3 blocks used, a parsing, finish and fail blocks. The completion, finish and fail blocks are all causing a retain cycle.

I know that the cause is references to ivars within the block but what I’ve tried hasn’t worked, see the end of the post for what I tried.

The following code is as it was before I started trying to fix it.The code path goes as follows:

1: Create the request:

//in the MainViewController.m
SHRequest *request = [api userInfo];

2: The method that creates the SHRequest

//in API.m
-(SHRequest*)userInfo{

    FROAuthRequest *request = [[FROAuthRequest alloc]initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",SH_URL_API,SH_URL_USER_INFO]] 
                                                    consumer:consumer 
                                                       token:token 
                                                       realm:nil 
                                           signatureProvider:signatureProvider];

    //wrap the FROAuthRequest in our custom object
    //see below for the SHRequest 
    SHRequest *shRequest = [[SHRequest alloc]initWithRequest:request];

    //set the parsing block
    shRequest.parsingBlock = ^id(FROAuthRequest* finishedRequest){

        NSDictionary *jsonResponse = [finishedRequest.responseData objectFromJSONData];

        [user release];
        user = [[SHUser alloc]initWithJSON:[jsonResponse objectForKey:@"user"]];

        //more code

        return [NSDictionary dictionaryWithObjectsAndKeys:user,kUserKey,nil];
   };

   [request release];
   return [shRequest autorelease];
}

3: The SHRequest

//in SHRequest.m
-(id)initWithRequest:(FROAuthRequest*)_underlyingRequest{
    if(self = [super init]){
        underlyingRequest = [_underlyingRequest retain];

        //this is the majority of the post processing
        underlyingRequest.completionBlock = ^{
            //if the requests fails call the fail block
            if(underlyingRequest.responseStatusCode != 200){
                if(failBlock != nil)
                    failBlock();
                return;
            }

            if([underlyingRequest.responseData length] > 0){
                [object release];
                object = parsingBlock(underlyingRequest);
                [object retain];

                if((underlyingRequest.error || !object) && failBlock != nil)
                    failBlock();
                else if(finishBlock != nil)
                    finishBlock();
            }else if(failBlock != nil)
                failBlock();
        };

        underlyingRequest.failedBlock = ^{
            if(failBlock)
                failBlock();
        };
    }
return self;
}

4: Once the SHRequest is returned from the userInfo method (1) the finish and fail blocks are set. (For this instance no failBlock is set.

//in MainViewController.m
request.finishBlock = ^{
    NSDictionary *userInfo = request.object;

    //User
    SHUser *user = [userInfo objectForKey:kUserKey];

    //more code

};
[request send];

Here’s what I have tried
I moved the completionBlock code to a method that starts the request and used __block types and the leaks appear to have disappeared but some of the __block vars are zombies when the completion block runs.

//in SHRequest.m
-(void)send{

    __block void(^fail)(void) = failBlock;
    __block void(^finish)(id) = finishBlock;
    __block id(^parsing)(FROAuthRequest*) = parsingBlock;
    __block FROAuthRequest *req = underlyingRequest;

    underlyingRequest.completionBlock = ^{

        if(req.responseStatusCode != 200){
            if(fail != nil)
                fail();
            return;
        }
        if([req.responseData length] > 0){
           id obj = parsing(req);//<--- parsing is a zombie 

            if((req.error || !obj) && fail != nil)
                fail();
            else if(finish != nil)
                finish(obj);//<--- finish is a zombie
        }else if(fail != nil)
            fail();
    };

    underlyingRequest.failedBlock = ^{
         if(fail)
             fail();
    };

    [underlyingRequest startAsynchronous];
}

Any ideas on what I’m doing wrong?

  • 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-06-09T09:49:26+00:00Added an answer on June 9, 2026 at 9:49 am

    Copying the parsing/finish/fail blocks and passing the request object as a param for the block seems to have sorted out my problem

    -(void)send{
    
    __block void(^fail)(void) = [failBlock copy];
    __block void(^finish)(id) = [finishBlock copy];
    __block id(^parsing)(FROAuthRequest*) = [parsingBlock copy];
    __block FROAuthRequest *req = underlyingRequest;
    
    underlyingRequest.completionBlock = ^{
    
        if(req.responseStatusCode != 200){
            if(fail != nil)
                fail();
            return;
        }
        if([req.responseData length] > 0){
           id obj = parsing(req);
    
            if((req.error || !obj) && fail != nil)
                fail();
            else if(finish != nil)
                finish(obj);
        }else if(fail != nil)
            fail();
    };
    
    underlyingRequest.failedBlock = ^{
         if(fail)
             fail();
    };
    
    [underlyingRequest startAsynchronous];
    }
    

    and in any request where I need a reference to an ivar or the request itself I create a block var and retain it then release it inside the finish/fail blocks

    __block SHRequest *req = [request retain];
    request.finishBlock = ^(id object){
    
        NSDictionary *userInfo = object;
    
        //User
        SHUser *user = [userInfo objectForKey:kUserKey];
    
        //more code
        [req release];
    };
    
    request.failBlock = ^{
        if(req.requestStatusCode == 500)
            //do stuff
        [req release];
    };
    

    This way Instruments reports no more leaks.

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

Sidebar

Related Questions

I've been programming for over 20 years, but have recently turned to JavaScript. Despite
Recently I've been thinking about how to transform a complex polygon into a non-complex
Recently I was able to create a tablet software for my Cerebral Palsy girl
Recently I have released my app into the US AppStore. Now I Planed for
I've recently dropped use of Graphael and and extensions package Ico . I think
We recently went into private beta on our flagship product and had a small
Recently I have been dealing with windows LogonUser API. The LogonUser api returns different
Recently,I am handling a solution for WebPart internationalization,but I am not familiar with the
Recently I've been doing quite the project mostly working with the DateTime class. Now,..
A user recently notified me that whenever they attempt to dial into a conference

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.