UIScrollView bug? float foo = scrollview.zoomScale

2019-05-06 06:05发布

问题:

In method viewForZoomingInScrollView: of the delegate to my scrollview I do the seemingly innocent:

// scrollView is the parameter passed to this method

float foo = scrollView.zoomScale

Boom! Crash, hello gdb.

Is this a known bug? Should I submit it?

Cheers, Doug

回答1:

I get this and I thought it was because accessing zoomScale ends up calling viewForZoomingInScrollView, resulting in infinite recursion.

But I'm just speculating...



回答2:

It would be helpful to see the output in xcode's debugger console window. Usually when you get a crash, there is some extra information available from the debugger console (open with command-shift-R, or Run > Console in the menu). If there was an exception that caused the crash, it will say which one. In any case, you can type bt (for backtrace) right after a crash and see the call stack when the crash occurs.

In your particular case, it's possible that you've accidentally released the UIScrollView object, but still have a pointer to where the old deallocated object was. This would give you a crash on the next call to any method in UIScrollView, and since zoomScale is a getter accessor, it counts as a method call. The most obvious symptom of this problem would be an EXC_BAD_ACCESS exception in the debugger console when the crash occurs.



回答3:

Doug thanks for that fix. Why didn't I think of that? >_<

And just as the OP said, grabbing the zoomScale property fires viewForZoomingInScrollView everytime.

Pretty much, don't use the zoomScale property inside viewForZoomingInScrollView, if you do? CRASH!!!!



回答4:

As moshy said, the reason the reason this happens is because the zoomScale property involves a call to viewForZoomingInScrollview, thus causing an infinite method call.