Skip to content
This repository has been archived by the owner on Jun 2, 2018. It is now read-only.

Crash when using a block with a UIButton #5

Closed
siancu opened this issue Jun 30, 2011 · 6 comments
Closed

Crash when using a block with a UIButton #5

siancu opened this issue Jun 30, 2011 · 6 comments

Comments

@siancu
Copy link
Contributor

siancu commented Jun 30, 2011

Hi,

I've added a UIButton to my code and I've added an event handler to it. How, when I am pressing the button, the code crashes with EXC_BAD_ACCESS.

I've ran the test app in Instruments with Zombies selected and I've noticed that the problem was in UIControl+BlocksKit.m, lines 31-34. I've also debugged the issue and it seems that the instance of the BKControlWrapper is released twice.

So I've managed to fix the issue by adding a BK_RETAIN(target) here:

- (void)addEventHandler:(BKSenderBlock)handler forControlEvents:(UIControlEvents)controlEvents {
    NSMutableArray *actions = [self associatedValueForKey:&kControlBlockArrayKey];

    if (!actions)
        [self associateValue:[NSMutableArray array] withKey:&kControlBlockArrayKey];

    BKControlWrapper *target = [BKControlWrapper wrapperWithHandler:handler forControlEvents:controlEvents];
    BK_RETAIN(target);
    [actions addObject:target];
    [self addTarget:target action:@selector(invoke:) forControlEvents:controlEvents];
}

I've ran the app in Instruments with Leaks after this change and there are no leaks.

Please add this fix into the code.

Thanks,
Stelian

@zwaldowski
Copy link
Collaborator

I'll fix it "better" later on, but it's fixed for now. Thanks for the contribution!

@siancu
Copy link
Contributor Author

siancu commented Jul 4, 2011

It still crashes, after your latest changes. The problem seems to be the BK_RELEASE(target) in addEventHandler:forControlEvents. I think the problem is that you're releasing target there, but you add it as a target to the UIControl a line above, and when it comes to the point where the invoke: selector is to be called, target is already released.

I've commented out BK_RELEASE(target) on line 64 and it works but it leaks.

Any ideas?

Thanks,
S.

@zwaldowski
Copy link
Collaborator

I'll investigate again, but it should really be retained by being
added to that NSMutableArray, so I suppose the issue is with that.

On Jul 4, 2011, at 9:24 AM, siancu
[email protected]
wrote:

It still crashes, after your latest changes. The problem seems to be the BK_RELEASE(target) in addEventHandler:forControlEvents. I think the problem is that you're releasing target there, but you add it as a target to the UIControl a line above, and when it comes to the point where the invoke: selector is to be called, target is already released.

I've commented out BK_RELEASE(target) on line 64 and it works but it leaks.

Any ideas?

Thanks,
S.

Reply to this email directly or view it on GitHub:
#5 (comment)

@siancu
Copy link
Contributor Author

siancu commented Jul 4, 2011

I've fixed it in my fork, see here: https://github.com/siancu/BlocksKit/commit/6508502b1a4fc34620b50cf15cc8a54018ef8885

I could open a pull request if you wish.

@zwaldowski
Copy link
Collaborator

I know it would be like that, ahaha. Thanks! Yeah, go ahead and send a
pull request.

On Jul 4, 2011, at 9:46 AM, siancu
[email protected]
wrote:

I've fixed it in my fork, see here: https://github.com/siancu/BlocksKit/commit/6508502b1a4fc34620b50cf15cc8a54018ef8885

I could open a pull request if you wish.

Reply to this email directly or view it on GitHub:
#5 (comment)

@siancu
Copy link
Contributor Author

siancu commented Jul 4, 2011

Sent it: #9

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants