首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iOS SIGSEGV SEGV_ACCERR在layoutSubviews中坠毁

iOS SIGSEGV SEGV_ACCERR在layoutSubviews中坠毁
EN

Stack Overflow用户
提问于 2014-10-09 08:22:36
回答 1查看 2.7K关注 0票数 1

我的应用程序有时会在layoutSubview方法中崩溃。当应用程序计算UITableViewCell的高度时,就会发生这种情况。有时应用程序崩溃,但并不总是如此。在这里,函数代码:

代码语言:javascript
运行
复制
+ (CGFloat)heightForEditorialCommentData:(EditorialCommentVMData *)editorialData
{
    EditorialCommentCell * cell = [EditorialCommentCell instanceCell];
    [cell applyViewModelData:editorialData];
    [cell layoutSubviews];
    return cell.commentTextLabel.frameBottom + kTextInsets.bottom;
}

+ (EditorialCommentCell *)instanceCell {
    static EditorialCommentCell *instance = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSArray *views = [[NSBundle mainBundle] loadNibNamed: @"EditorialCommentCell" owner:0 options:0];
        instance = views[0];
        instance.frame = CGRectMake(instance.contentView.frame.origin.x,
                                        instance.contentView.frame.origin.y,
                                        [[UIScreen mainScreen] bounds].size.width,
                                        instance.contentView.frame.size.height);
    });

    return instance;
}

下面是堆栈跟踪:

代码语言:javascript
运行
复制
Thread : Crashed: com.apple.root.default-qos
 0  UIKit                          0x2ff92b3a -[_UIViewAdditiveAnimationAction runActionForKey:object:arguments:] + 569
 1  UIKit                          0x2ff92cf5 __67-[_UIViewAdditiveAnimationAction runActionForKey:object:arguments:]_block_invoke + 408
 2  QuartzCore                     0x2f71e4c5 CA::Layer::end_change(CA::Transaction*, unsigned int, objc_object*) + 96
 3  QuartzCore                     0x2f71f06b CA::Layer::set_bounds(CA::Rect const&, bool) + 530
 4  QuartzCore                     0x2f71ed87 -[CALayer setBounds:] + 110
 5  UIKit                          0x3017f84f -[_UILabelLayer setBounds:] + 58
 6  QuartzCore                     0x2f71fe01 -[CALayer setFrame:] + 600
 7  UIKit                          0x3017f7eb -[_UILabelLayer setFrame:] + 58
 8  UIKit                          0x2fcfc4d7 -[UIView(Geometry) setFrame:] + 254
 9  UIKit                          0x2fd09f53 -[UILabel setFrame:] + 138
 CRASH->10 MYAPP                     0x000fb611 -[UIView(Frame) setFrameWidth:] (UIView+Frame.m:103)
 11 MYAPP                     0x00130c75 -[EditorialCommentCell layoutSubviews] (EditorialCommentCell.m:139)
 12 MYAPP                     0x001324ed +[EditorialCommentCell heightForEditorialCommentData:] (EditorialCommentCell.m:351)
 13 MYAPP                     0x00115733 -[EditorialCommentsViewModel applyDataFromItem:toVMItem:] (EditorialCommentsViewModel.m:298)
 14 MYAPP                     0x00114e73 -[EditorialCommentsViewModel vmDataFromItem:] (EditorialCommentsViewModel.m:222)
 15 MYAPP                     0x000fac29 -[FatherViewModel insertItemsUpdatingExisting:intoVMItems:] (FatherViewModel.m:418)
 16 MYAPP                     0x000f9cfd __39-[FatherViewModel updateItems:failure:]_block_invoke_3 (FatherViewModel.m:281)
 17 libdispatch.dylib              0x3a4718cb _dispatch_call_block_and_release + 10
 18 libdispatch.dylib              0x3a47ada3 _dispatch_root_queue_drain + 834
 19 libdispatch.dylib              0x3a47bcd7 _dispatch_worker_thread3 + 94
 20 libsystem_pthread.dylib        0x3a5d2e31 _pthread_wqthread + 668

这里是我的layoutSubview代码:

代码语言:javascript
运行
复制
- (void)layoutSubviews {
    [super layoutSubviews]; // Sometimes crashed here

    CGFloat contentLeft = kTextInsets.left;

    BOOL isReply = self.editorialCommentData.inReplyToCommentId != 0;

    if (isReply) {
        contentLeft += kAuthorLeftInset + self.authorImageView.frameWidth;
    }

    self.authorImageView.frameOrigin = CGPointMake(contentLeft, kContentInsets.top);

    self.votesLabel.frameWidth = 200; // Sometimes crashed here
    [self.votesLabel sizeToFit];

    self.votesLabel.frameTop = self.authorImageView.frameTop - 1.5f;
    self.votesLabel.frameRight = self.contentView.bounds.size.width - kContentInsets.right;

    <.....more code here.....>
}

我认为这是内存管理问题。

EN

Stack Overflow用户

回答已采纳

发布于 2014-10-09 08:41:09

好的!我修好了。马可对layoutSubviews方法的评论:“您不应该直接调用此方法。如果您想强制进行布局更新,请在下一次绘图更新之前调用setNeedsLayout方法。”我刚刚将layoutSubviews更改为layoutIfNeeded方法:

代码语言:javascript
运行
复制
+ (CGFloat)heightForEditorialCommentData:(EditorialCommentVMData *)editorialData
{
    EditorialCommentCell * cell = [EditorialCommentCell instanceCell];
    [cell applyViewModelData:editorialData];
    [cell layoutIfNeeded];
    return cell.commentTextLabel.frameBottom + kTextInsets.bottom;
}

来自苹果关于layoutIfNeeded的文档:

使用此方法在绘图前强制进行子视图的布局。使用接收消息的视图作为根视图,此方法从根开始布局视图子树。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26273621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档