Override here?

I get EXC_BAD_ACCESS on some devices in ad hoc beta for my application, see here: iPhone application debugging help - EXC_BAD_ACCESS

I managed to use atos -arch armv6 -o myapp.app/myapp 0x000037a6 in the terminal to track the method that causes this problem, and this led me to this part of the code:

for (UIView *view in scrollView.subviews) {
    [view removeFromSuperview];
}

I suspect the application receives a memory access warning and releases scrollview or UIImageViews that are children, so when I use this method above, it encounters an error (and crash) as it overrides the view.

My question is, how can I make it safe so that it is released only if it has not yet been released?

+3
source share
1 answer

You modify the array while you iterate over it. It's thin, but since it removeFromSuperviewremoves it from the list of subheadings, you are changing the array. Change your code to this,

NSArray *subviews = [scrollView.subviews copy];
for (UIView *view in subviews) {
    [view removeFromSuperview];
}
[subviews release];

and everything should be fine.

+6
source

Source: https://habr.com/ru/post/1727709/


All Articles