【问题标题】:why did this happen when objective-c has conflict return type?为什么当objective-c 有冲突返回类型时会发生这种情况?
【发布时间】:2016-08-31 02:35:12
【问题描述】:

问题来了:我想在iOS设备上显示类似gif效果的序列图,并且我使用UIImageView动画功能,所以实现以下方法:

// Animation.m
// @files:     diagrams' array
// @fileDir:   file directory
// @imageview: Just UIImageView
//
- (void)showSeqDiagram:(NSArray*)files
            fileDirect:(NSString*)fileDir
             imageView:(UIImageView*)imageview
{
    int cnt = [files count];
    NSMutableArray * images = [NSMutableArray arrayWithCapacity:cnt];
    for (id file in files)
    {
        NSString * imageName = [NSString stringWithFormat:@"%@/%@", fileDir, file];
        UIImage * image = [UIImage imageNamed:imageName];

        if(!image)
            continue;
        [images addObject:image];
    }
    imageview.animationImages = images;

    imageview.animationDuration = 5;
    imageview.animationRepeatCount = 1;
    [imageview startAnimating];
}

到目前为止,一切都很顺利。我在头文件中声明 return type 时犯了一个错误,如下所示:

//Animation.h
- (UIImageView*)showSeqDiagram:(NSArray*)files
                    fileDirect:(NSString*)fileDir
                     imageView:(UIImageView*)imageview;

我在 - (void)loadView 中调用这个方法

- (void)loadview
{
......
[self showSeqDiagram:files fileDirect:fileDir imageView:imageview];
......
}

然后在调用位置崩溃并在iPhone5模拟器和设备iOS verison9.3上抛出EXC_BAD_ACCESS(code=1, );

iPhone6模拟器,iOS version9.3偶尔崩溃;

在 iPhone6/6plus 设备和 iPhone6plus 模拟器上从未崩溃,在 iOS 7-9 上测试。

我知道这是错误的,但想知道为什么会发生这种情况,有人可以解释一下吗?先谢谢了~

【问题讨论】:

  • 您从哪里获取 imageView,它是在故事板/Xib 中还是以编程方式?
  • 修改返回类型,使.h和.m中的方法相同。尝试设置断点以找出该方法(行)的确切问题所在,或者是因为方法声明与其实现不匹配。
  • 你可以尝试在 *.h 和 *.m 中删除不同的返回值,编译器只是抛出一个警告,它也可以在其他文件中调用并且它们确实匹配。试试看!

标签: ios objective-c uiimageview crash return


【解决方案1】:

嗯,我的同事已经回答了,以下是详细信息:

  • 首先是 2 背景:
  • arm-arch 将方法返回值存储在 %rax 寄存器中;
  • 并且对象不会在 ARC 模式下立即释放,并且 _obj_release_XXX 在 RunLoop 中具有最低优先级

所以 showSeqDiagram 不会返回任何值,所以 %rax 寄存器没有值,但是 autoReleasePool 会在短时间内尝试释放 %rax 中的 "UIImageView" obj(优先级最低)。在这么短的时间内,如果其他 RunLoop 中的其他方法覆盖了 %rax,那么它不会崩溃;否则,如果没有人重写 %rax,就会发生崩溃。

这可以部分解释为什么随机崩溃。有更好的答案吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-28
    • 2021-10-22
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 2021-04-30
    相关资源
    最近更新 更多