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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:35:49+00:00 2026-05-25T15:35:49+00:00

I’m having an issue with memory management when dealing with callbacks and async code

  • 0

I’m having an issue with memory management when dealing with callbacks and async code in objective c.
I cant seem to find a way to release the instance that the callback is set on.

For example:

MyClass *myArchive = [[MyClass alloc] init]  ;
[myArchive callBack:^(RKObjectLoader* objectLoader, id object ) {

    NSLog(@"success");

} fail:^(RKObjectLoader* objectLoader, NSError* error) {

    NSLog(@"failed");


}];

[myArchive searchArchive:words:paging];

The problem being that I don’t know when or how to release the instance *myArchive. Using Instruments within xcode to profile my code I always get a leak here. The function searchArchive performs an async request to a server using restkit. I wont reference the instance from within the callback as I heard this causes a retain cycle and I have done some reading about using __block and other c approaches to avoid retain cycles which is all fine but as it stands now with no actual code happening within the callback how do I release the *myArchive instance. anyone able to explain how I should deal with this within objective-c?

EDIT:

This is where I set the callback in myclass

// Sets internal backs on this object which basically wrap the delegate 
//
- (void)callBack: (void (^)(RKObjectLoader* objectLoader, id object))success 
            fail: (void (^)(RKObjectLoader* objectLoader, NSError* error))fail {
    //sanity check
    NSAssert(_currentDelegate != self, @"Delegate is another object.  Can not set callback");

    // store our callback blocks in the instance

    _success = [success copy]  ;
    _fail = [fail copy]  ;
}

and then release _success and _fail in dealloc

and within the @interface

@interface myClass : NSObject<RKObjectLoaderDelegate> { 
    // holds the block callback for "success"
    void (^_success)(RKObjectLoader* objectLoader, id object);
    // holds the block callback for "fail"
    void (^_fail)(RKObjectLoader* objectLoader, NSError* error);
}

I hope this gives more insight into what I’m doing wrong.

EDIT 2:

Ok I’m beginning to see the errors now:

-(void)retrieveGallery{    

    //create call back for async and deal with the result
    [_galleryItems callBack:^(RKObjectLoader* objectLoader, NSArray *objects) {

        //success happy days. do a bunch of code here that does not cause leaks

    } fail:^(RKObjectLoader* objectLoader, NSError* error) {
        //retry the attempt to retrieve gallery data from the server 
        _retryCount++;
        if (_retryCount < _maxRetryCount) {
            [self retrieveGallery];
        }


    }];

    //read the collection of gallery items from server   
    [_galleryItems readGallery];

 }

The only actual memory leaks are when the callback catches a fail for what ever reason and then calls the [self retrieveGallery] function from within callback to attempt again. this is what is causing the leak so I’m guessing that is a big no no. How should I attempt the function (retrieveGallery in this case) again.

  • 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-25T15:35:49+00:00Added an answer on May 25, 2026 at 3:35 pm

    Memory management isn’t really any different because you are using an asynchronous callback. myArchive should be a property of whatever class you are doing this in. You want it to stick around until the task is complete, right?

    @property (retain) MyClass *myArchive;
    

    Then..

    myArchive = [[MyClass alloc] init];
    
    void (^on_success_callback)(void) = ^(void){
        NSLog(@"success");
        self.myArchive = nil;
    };
    

    You need to make sure you are managing the callbacks properly, i.e. copying them from the stack and releasing them when you are done.

    If you have retains and releases in your code you probably aren’t using the accessor methods properly.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.