Xcode升级到4.5后应用程序崩溃。将保留对象分配给unsafe_unretained变量 [英] App crashes after Xcode upgrade to 4.5. Assigning retained object to unsafe_unretained variable

查看:718
本文介绍了Xcode升级到4.5后应用程序崩溃。将保留对象分配给unsafe_unretained变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我班上,我有一个 dispatch_queue_t 属性,声明如下:

In my class I have a dispatch_queue_t property declared like this:

@property (nonatomic, assign) dispatch_queue_t queue;

然后在我的init方法中我做:

Then in my init method I do:

- (id)initWithServerUrls: (NSString*)serverUrls
{
    if (self = [super init])
    {
        _queue = dispatch_queue_create("com.xxx.my_send_queue", DISPATCH_QUEUE_SERIAL);
    }

    return self;
}

在Xcode 4.4.1中它起作用并且没有引起任何问题(应用程序)在appstore中测试了+)。
现在我升级到Xcode 4.5后,应用程序崩溃了 EXC_BAD_ACCESS ,Xcode在该行上发出警告说:

In Xcode 4.4.1 it worked and did not cause any problems (the app tested + in the appstore). Now after I upgraded to Xcode 4.5 the app crashes with EXC_BAD_ACCESS and Xcode gives me a warning on that line saying:

将保留对象分配给unsafe_unretained变量;对象将在分配后发布

Apple将Xcode 4.5中的编译器从LLVM 4.0更新为LLVM 4.1但我不知道为什么我的代码现在崩溃了。

Apple updated the compiler in Xcode 4.5 from LLVM 4.0 to LLVM 4.1 but I have no clue why my code is crashing right now.

我介入了代码,崩溃发生在该行之后。
您有什么想法可能有什么问题吗?我该如何解决?

I stepped through the code and the crash happens just after that line. Do you have any ideas what can be wrong and how can I fix it?

解决方案:

我设法让它与两个SDK一起使用。我刚补充说:

I managed to get it working with both SDKs. I just added:

#if OS_OBJECT_USE_OBJC
@property (nonatomic, strong) dispatch_queue_t queue; // this is for Xcode 4.5 with LLVM 4.1 and iOS 6 SDK
#else
@property (nonatomic, assign) dispatch_queue_t queue; // this is for older Xcodes with older SDKs
#endif

希望有人觉得它很有用

推荐答案

首先,如果您的目标平台是5+,那么我强烈建议您使用iOS 5 SDK进行构建。使用更高版本的SDK构建并设置目标可以工作,但是有很多问题(其中最重要的是你没有编译器帮助找到你使用不支持的方法的地方)。所以答案1:你需要iOS 5,针对iOS 5进行构建,这无关紧要。

First, if your target platform is 5+, then I strongly recommend building with the iOS 5 SDK. Building with a later SDK and setting the "target" can work, but has lots of problems (not the least of which is that you get no compiler help to find places that you've used unsupported methods). So answer 1: You need iOS 5, build against iOS 5 and this shouldn't matter.

在iOS 6中, dispatch_queue_t 是一个ObjC对象。这是一个很大的改进。这意味着您可以为它创建 strong 属性,ARC将负责其余的工作。如果你的目标是iOS 6,这应该可以正常工作。

In iOS 6, dispatch_queue_t is an ObjC object. This is a great improvement. It means you can just create strong properties for it and ARC will take care of the rest. If you target iOS 6, this should just work.

如果你需要为iOS 5和iOS 6构建相同的代码,那么你需要知道哪个是哪个您可以在需要时将其放入内存管理中,而在不需要时将其保留。要使用的正确测试是 #if OS_OBJECT_USE_OBJC 。请记住,这是编译时检查。它仅适用于处理您要针对不同SDK编写的代码。对于给定的SDK,行为将是一种或另一种。

If you need to build the same code for iOS 5 and iOS 6, then you need to know which is which so that you can put in the memory management when you need it and leave it out when you don't. The correct test to use is #if OS_OBJECT_USE_OBJC. Remember, this is a compile-time check. It's only applicable for dealing with code you want to write against different SDKs. For a given SDK, the behavior will aways be one way or the other.

关于unsafe_unretained与assign混淆:在这种情况下它们是相同的。 assign仅适用于非对象。 unsafe_unretained是应用于对象时转换为assign的内容。在iOS6中, dispatch_queue_t 是一个对象。

Regarding the "unsafe_unretained" versus "assign" confusion: They are the same thing in this case. "assign" only applies to non-objects. "unsafe_unretained" is what "assign" is converted to when applied to objects. And in iOS6, dispatch_queue_t is an object.

还有一个解决方法,特别是如果你真的想要保留使用iOS 6 SDK构建旧的内存管理代码。您可以将 -DOS_OBJECT_USE_OBJC = 0 传递给编译器。这将选择退出新模型。但我会建议这作为最后的手段。有关详细信息,请参阅SDK中的 os / object.h 。 (Cmd-Shift-O,object.h)

One more workaround, particularly if you really do want to keep the old memory management code while building with the iOS 6 SDK. You can pass -DOS_OBJECT_USE_OBJC=0 to the compiler. This will opt-out of the new model. But I would recommend this as a last resort. For details, see os/object.h in the SDK. (Cmd-Shift-O, object.h)

这篇关于Xcode升级到4.5后应用程序崩溃。将保留对象分配给unsafe_unretained变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆