Skip to content
This repository has been archived by the owner on Nov 29, 2022. It is now read-only.

Crash when deallocating #47

Closed
guilhermearaujo opened this issue Apr 20, 2015 · 5 comments
Closed

Crash when deallocating #47

guilhermearaujo opened this issue Apr 20, 2015 · 5 comments

Comments

@guilhermearaujo
Copy link

I am trying to use this Control on a Storyboard. My view controller is arranged this way:
UIViewController > UIView (Main) > UIView (Gradient BG) > UIView (DZNSegmentedControl)

If I push that view controller and then pop it, the app crashes with this log message (note: zombies enabled):

*** -[UIView release]: message sent to deallocated instance 0x7fedeae64ac0

If I remove the class name from that view, the app will no longer crash.
If the view assigned as DZNSegmentedControl is not inside a second view (the gradient), but is inside the main view, it also will not crash.

I created a sample project that demonstrates this.
The project has a second branch called programmatically, when I keep the inner view set as a regular UIView and then add a DZNSegmentedControl inside it programmatically. that way the app will not crash.

@SofteqDG
Copy link

You need to check newSuperview in the willMoveToSuperview method to fix this issue.
This fix works for me

- (void)willMoveToSuperview:(UIView *)newSuperview
{
    [super willMoveToSuperview:newSuperview];

    if (newSuperview) {

        [self layoutIfNeeded];
    }
}

@dzenbot
Copy link
Owner

dzenbot commented May 21, 2015

👍 this crash isn't really related to DZNSegmentedControl code tho, is it?

@SofteqDG
Copy link

This fix is for willMoveToSuperview method of the DZNSegmentedControl.
I really don't know why DZNSegmentedControl crashes without this check (maybe system issue?)
But it is logical that not necessary to call layoutIfNeeded method if newSuperview is nil :)

@dzenbot
Copy link
Owner

dzenbot commented May 22, 2015

What if we override layoutIfNeeded and added this following implementation:

- (void)layoutIfNeeded
{
    if (!self.superview) {
        return;
    }

    [super layoutIfNeeded];
}

Do you think that would solve it?

@dzenbot
Copy link
Owner

dzenbot commented May 22, 2015

Based on the documentation:

A view object that will be the new superview of the receiver. This object may be nil.

So your fix it pretty much legit. I looks like willMoveToSuperview: is still called when removing from superview.

dzenbot pushed a commit that referenced this issue May 22, 2015
@dzenbot dzenbot closed this as completed May 22, 2015
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

3 participants