I have a paginating scrollview with UIImageView’s inside.
At a point i need to remove some Imageviews to avoid memory-problems.
But at any point i just get a BAD_ACCESS .
for(UIView *subview in [scrollView subviews]) {
if([subview isKindOfClass:[UIImageView class]]) {
if ( ([subview tag] != ActualPage) && ([subview tag] != (ActualPage - 1)) && ([subview tag] != (ActualPage+1)) )
{
[subview removeFromSuperview];
}
}
}
Basically i wanna remove every subview except the actual page, one back and one forward.
When you have a crash, it is helpful to post the backtrace. Is the crash happening in that loop or elsewhere?
If that loop is executing during display, then you are violating the contract of
-removeFromSuperviewwhich explicitly states that it should not be called in the middle of display.I could also imagine a crash related to causing the
subviewsto mutate in the middle of enumeration.Without a backtrace, it is impossible to say conclusively.
In general, if you have code that is using
isKindOfClass:for behavior such as this, you have a design oddity that ought to be revisited.