UIImageView crash while trying to set frame

First up is my crash log:

Topic 0 Crashed:
0 libSystem.B.dylib 0x35176264 __kill + 8
1 libSystem.B.dylib 0x35176254 kill + 4
2 libSystem.B.dylib 0x35176246 raise + 10
3 libSystem.B.dylib 0x3518ad02 abort + 50
4 libstdc ++. 6.dylib 0x31432a20 __gnu_cxx :: __ verbose_terminate_handler () + 376
5 libobjc.A.dylib 0x31a97594 _objc_terminate + 104
6 libstdc ++. 6.dylib 0x31430df2 _cxxabiv1 :: _ terminate (void (*) ()) + 46
7 libstdc ++. 6.dylib 0x31430e46 std :: terminate () + 10
8 libstdc ++. 6.dylib 0x31430f16 __cxa_throw + 78
9 libobjc.A.dylib 0x31a964c4 objc_exception_throw + 64
10 CoreFoundation 0x361857c2 + [NSException raise: format: arguments:] + 62
11 CoreFoundation 0x361857fc + [NSException raise: format:] + 28
12 QuartzCore 0x3148b222 CALayerSetPosition (CALayer *, CA :: Vec2 const &, bool) + 134
13 QuartzCore 0x3148b190 - [CALayer setPosition:] + 32
14 QuartzCore 0x3148b0dc - [CALayer setFrame:] + 384
15 UIKit 0x35d15aba - [UIView (Geometry) setFrame:] + 182
16 UIKit 0x35d15928 - [UIImageView setFrame:] + 96

I copied part from the crash log. After line 16, there are my classes, which I cannot present here. In MyClass and MyMethod, I change the frame of the imageView. My problem is that I cannot reproduce this error and I want to reproduce it. What causes this journal? I tried to free the imageView before calling setFrame: but it does not cause this error. Any ideas how to get this? Or why does this error happen several times?

+4
source share
1 answer

I understand this problem from one of my own projects. Usually when setFrame: crashes because you are trying to set NaN (not a number). I don’t know if you have practiced NaN before, but if you haven’t left a comment, I will provide information on how to deal with it.

EDIT: It's time to think that I can give you an example.

So, here is a sample code to explain why the error is hard to reproduce and how to fix it. I don’t know what your code looks like, but your problem sounds so much that I believe that you made the same mistake as me.

Consider the following code:

 - (void)layoutSubviews { CGRect imageFrame; switch (self.state) { case 0: imageFrame = CGRectMake(0, 0, 100, 100); case 1: imageFrame = CGRectMake(10, 10, 50, 50); } self.imageView.frame = imageFrame; } 

We believe that self.state is 2, then the imageFrame will never be initialized and will contain everything that was in this memory location, possibly NaN. The reason this is difficult to reproduce is because a failure will only occur when NaN is in this cell.

In my example, the error is very easy to determine, and it is probably not easy to notice in your code. If you can’t find it on your own, feel free to post your code and I’ll take a look at it. If you have any questions, feel free to leave comments.

+7
source

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


All Articles