Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImageView crash when trying to set the frame

First here's my crash log:

Thread 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 have copied a 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 an imageView. My problem is that I cannot reproduce this bug and I want to reproduce it. What causes this log? I tried to release the imageView before I call the setFrame:, but it doesn't produce this error. Any ideas how to get it? Or why this error happens some times?

like image 252
Infinite Possibilities Avatar asked Feb 24 '23 19:02

Infinite Possibilities


1 Answers

I recognize this problem from one of my own projects. Usually when setFrame: crashes it's because your trying to set a NaN (Not a number). I don't know if you've dealt with NaN before, but if you haven't drop a comment and I'll provide information on how to deal with it.

EDIT: Had some time and thought I might give you an example.

So here's a code example to explain why the bug is hard to reproduce and how to fix it. I don't know what your code looks like, but your problem sounds similar enough to make me believe that you've done the same mistake as I did.

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;
}

Consider that self.state is 2, then imageFrame will never be initialized and will contain whatever was on that memory location, possibly NaN. The reason why this is hard to reproduce is that the crash will only occur when there is NaN on that memory location.

In my example the error is very easy to spot and it's likely that it's not as easy to spot in your code. If you can't find it yourself, feel free to post your code and I'll take a look at it. If you have any questions, don't hesitate to leave a comment.

like image 53
Erik B Avatar answered Mar 15 '23 17:03

Erik B