-1

I am written below code in a button click function.

- (IBAction)btnPlusClicked:(id)sender forEvent:(UIEvent *)event {
    //show popover controller

    TSActionSheet* actionSheet = [[TSActionSheet alloc] initWithTitle:nil];

    [actionSheet addButtonWithTitle:@"Add New" block:^{
        //add new button clicked
        [self showAddNewView:0];
    }];

    [actionSheet addButtonWithTitle:@"Copy Yesterday" block:^{
        //Copy yesterday clicked
        [self showAddNewView:1];
    }];

    [actionSheet addButtonWithTitle:@"Copy from Date" block:^{
        //Copy from date clicked
        //show date picker
        [self showDatePicker];
    }];

    actionSheet.cornerRadius = 5;

    [actionSheet showWithTouch:event];
}

when above code executes then the -(void)dealloc method of this view controller does not fire on below function of back button touch up inside event.

- (IBAction)btnBackClicked:(id)sender {
    [self.navigationController popViewControllerAnimated:animated];
}

-(void)dealloc
{
    NSLog(@"-->deallalloc in %@", [self class]);
} 

Note that app is ARC compatible.

is there any solution?

ujell
  • 2,732
  • 3
  • 15
  • 23
Coder_A_D
  • 340
  • 5
  • 20
  • You are creating a reference cycle. There are plenty of threads here and plenty of documentation on Apple's site how to avoid reference cycles. First read the documentation to understand the difference between a weak and a strong reference. – gnasher729 Jun 23 '14 at 11:15
  • that hidden strong reference-cycle is a good example why the external developers' code may not be trustful. but why do you have a strong reference to your `self` in your blocks, anyway? – holex Jun 23 '14 at 11:20
  • I also tried using __weak and __block – Coder_A_D Jun 23 '14 at 11:24
  • __weak TSActionSheet* actSheet = actionSheet; [actSheet addButtonWithTitle:@"Add New" block:^{ [self showAddNewView:0]; }]; [actSheet addButtonWithTitle:@"Copy Yesterday" block:^{ [self showAddNewView:1]; }]; – Coder_A_D Jun 23 '14 at 11:25
  • @gnasher729,holex-I got my issue by Avoid strong reference cycles. – Coder_A_D Jun 23 '14 at 12:38
  • possible duplicate of [Retain cycle on \`self\` with blocks](http://stackoverflow.com/questions/4352561/retain-cycle-on-self-with-blocks) – Sulthan Jun 23 '14 at 12:56
  • @gnasher729: where do you see a reference cycle? – newacct Jun 24 '14 at 00:58
  • @holex: where do you see a strong reference cycle? – newacct Jun 24 '14 at 00:58
  • @Sulthan: Why is it a duplicate? Is there a retain cycle here? – newacct Jun 24 '14 at 00:59
  • @newacct he answered his own question. the question might not give enough info but the answer makes it a duplicate. – Sulthan Jun 24 '14 at 06:28
  • @newacct, where do you **not** see the strong reference cycle? your logic is wrong here if you assume _"there is no string reference cycle while it is not proved"_. the correct logic is _"there is no strong reference cycle, if your prove it"_. therefore, there is a strong reference cycle (because no one proved the opposite yet), even if we don't see that in that tiny chuck of code. – holex Jun 24 '14 at 07:38
  • @holex: That's like saying, I am God, because no one has proved the opposite yet. The one making the claim has the burden of proving it. Plus here, it is very easy to see. Yes, each block has a strong reference to `self`, but how can `self` have a strong reference to the blocks? The blocks are only accessible by the action sheet. `actionSheet` is a local variable whose entire scope is visible in the code snippet. Nothing in that code would give `self` to get a reference to the action sheet. Unless you are counting things like swizzled the action sheet methods, in which case all bets are off. – newacct Jun 24 '14 at 09:44
  • @newacct, not at all, that is not a religion; that is pure science. that is how the development procedure works. _there is a potential bug until you can prove there is no bug._ guess, what would happen if every scientist starts to publish random theories, and they would be like _prove it if you don't agree_... there would be a chaos, because they have to prove that their own theory is correct, until it happens, the theory is **not** correct. the logic is the same for strong-reference-cycle too. – holex Jun 24 '14 at 09:56

1 Answers1

0

To avoid strong reference cycles we need to use weak reference as shown code below.

- (IBAction)btnPlusClicked:(id)sender forEvent:(UIEvent *)event {

    if(actionSheet == nil)
    {
        MyViewController *__weak weakSelf = self;

        actionSheet = [[TSActionSheet alloc] initWithTitle:nil];

        [actionSheet addButtonWithTitle:@"Add New" block:^{
            [weakSelf showAddNewView:0];
        }];

        [actionSheet addButtonWithTitle:@"Copy Yesterday" block:^{
            [weakSelf showAddNewView:1];
        }];
        [actionSheet addButtonWithTitle:@"Copy from Date" block:^{
            [weakSelf showDatePicker];
        }];
        actionSheet.cornerRadius = 5;
    }
    [actionSheet showWithTouch:event];

}

Coder_A_D
  • 340
  • 5
  • 20