Memory leak when using callback

I have a memory management issue when working with callbacks and asynchronous code in object c. I can't seem to find a way to free the instance in which the callback is set.

For instance:

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 is that I don't know when and how to release an instance of * myArchive. Using Tools in xcode to profile my code, I always get a leak here. The searchArchive function performs an asynchronous request to the server using restkit. I wonโ€™t refer to the instance inside the callback, since I heard that it causes a save loop, and I did some reading about using __block and other c-approaches to avoid saving loops, which is okay, but since it is standing now, without the actual code occurring within the callback, how can I free an instance of * myArchive. anyone who can explain how i should handle this in objective-c ?

EDIT:

Here 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 inside @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); } 

Hope this gives more information on what I'm doing wrong.

EDIT 2:

Good. Now I'm starting to see errors:

 -(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 failure for some reason, and then calls the [self retrieveGallery] function from the callback to try again. this is what causes the leak, so I guess this is a big no. How should I try to execute the function again (retrieveGallery in this case).

+4
source share
1 answer

Memory management is actually no different than using an asynchronous callback. myArchive should be a property of any class in which you do this. You want him to stick 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 that you manage the callbacks correctly, that is, copy them from the stack and free them when you are done.

If you saved and released in your code, you are probably using access methods incorrectly.

+1
source

All Articles