10.7 上的 ARC 迁移工具给出错误:删除未使用的“自动释放”是不安全的;信息

发布于 2024-12-03 17:09:36 字数 796 浏览 0 评论 0原文

我继承了在 10.6 上开发的应用程序,我想迁移到 10.7 上。我想遵守自动引用计数并且我开始了它。转换助手向我发送错误消息:“[重写器]删除未使用的“自动释放”消息不安全;它的接收者可能会立即被销毁”并指向以下方法:

+ (MyClass *)deserializeNode:(xmlNodePtr)cur
{
    MyClass *newObject = [[MyClass new] autorelease];

    [newObject deserializeAttributesFromNode:cur];
    [newObject deserializeElementsFromNode:cur];

    return newObject;
}

在旧的保留/释放环境中,这将是非常正常的样式(除了丑陋的“新”消息),但是,ARC环境不允许这样做。在我看来这不是很好的解决方案,但是我应该使用新指令创建民意调查,就像这样吗?这完全正确吗?

+ (MyClass *)deserializeNode:(xmlNodePtr)cur
{
    MyClass *newObject;
    @autorelease
    {
       newObject = [MyClass new];

       [newObject deserializeAttributesFromNode:cur];
       [newObject deserializeElementsFromNode:cur];
    }

    return newObject;
}

返回之前不会释放“newObject”吗?

I have inherited application developed on 10.6 and I want to migrate on 10.7. I would like to comply with Automatic Reference Counting and I started it. Conversion assistant is sending me and error message: '[rewriter] it is not safe to remove an unused 'autorelease' message; its receiver may be destroyed immediately' and points to following method:

+ (MyClass *)deserializeNode:(xmlNodePtr)cur
{
    MyClass *newObject = [[MyClass new] autorelease];

    [newObject deserializeAttributesFromNode:cur];
    [newObject deserializeElementsFromNode:cur];

    return newObject;
}

This would be pretty much normal style (except for ugly 'new' message) in old retain/release environment, however, ARC environment does not allow this. It doesn't seem to me very good solution, but should I create poll with new directive, like this? Is this correct at all?

+ (MyClass *)deserializeNode:(xmlNodePtr)cur
{
    MyClass *newObject;
    @autorelease
    {
       newObject = [MyClass new];

       [newObject deserializeAttributesFromNode:cur];
       [newObject deserializeElementsFromNode:cur];
    }

    return newObject;
}

Wouldn't that release 'newObject' before return?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

煮茶煮酒煮时光 2024-12-10 17:09:36

@autorelease 块只是围绕该代码部分创建一个新的自动释放池。它对该块内代码的实际内存管理没有任何作用。

我认为编译器试图向您指出的问题是,您从不遵循返回自动释放对象的方法的命名约定的方法返回自动释放的对象。

The @autorelease block just creates a new autorelease pool around that section of code. It doesn't do anything for the actual memory management of the code inside that block.

I think the problem the compiler is trying to point out to you is that you're returning an autoreleased object from a method that doesn't follow the naming convention for a method returning an auto-released object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文