UIView frame sent to deallocated instance

823 views Asked by At

I am currently working on an application that has some ViewController with a button on it that pushes to a UITableViewController with Search Bar and Search Display controller. I have some data in the cells and that gets populated. I have added the following code to hide the search bar and also make it clearcolor when you do see it:

[[self.searchBar.subviews objectAtIndex:0] removeFromSuperView];
[self.searchBar setBackgroundColor:[UIColor clearColor]];
CGRect newBounds = [[self tableView]bounds];
newBounds.origin.y = newBounds.origin.y + searchBar.bounds.size.height;
[[self tableView] setBounds:newBounds];

Now this works when I am using the iOS simulator, but when I run it on my device and begin to scroll, either up or down, it crashes and gives me the following error:

EXC_BREAKPOINT (code = EXC_ARM_BREAKPOINT, subcode = 0xdefe)

I then enabled Zombie Objects to further debug and got this:

-[UIView frame]: message sent to deallocated instance 0x156b1430

When I take off:

[[self.searchBar.subviews objectAtIndex:0] removeFromSuperView];
[self.searchBar setBackgroundColor:[UIColor clearColor]];

and run my application on my device again, it does not crash and works fine.

Anyone have any ideas whats going on and how this is happening? Thanks in advance!

3

There are 3 answers

1
h.kishan On
    [[self.searchBar.subviews objectAtIndex:0] removeFromSuperView]; 

    //This Line remove search bar from view ,which means it is deallocated at that moment so when you again try to remove it ,,the app crash ..
  //  So,instead of this try to hide searchbar ,,

    Self. searchBar.hidden =YES;
1
TopChul On

I think UISearchBar has unsafeunretained reference of [self.searchBar.subviews objectAtIndex:0];

And You set the Bounds of UITableView then change frame of UITableView. It would fire auto resizing and layoutSubviews.

So, UISearchBar's layoutSubviews method access [self.searchBar.subviews objectAtIndex:0].frame for layout or auto resizing.

It's not recommended that change view in UIControls from SDK. There is an idea that Setting hidden=YES instead removeFromSuperview.

0
shmim On

I was having this same problem. Got to this page, read the words 'zombie objects' in the question and remembered that I'd left zombie objects enabled. After disabling them, the problem went away. Now when I scroll my tableView, there's no crash.

This may be weird, but perhaps it will help someone...