【问题标题】:Automatic Reference Counting (ARC). Can ARC handle free-ing a plain ole c-array?自动引用计数 (ARC)。 ARC 可以处理释放一个普通的 ole c 数组吗?
【发布时间】:2012-10-20 01:55:40
【问题描述】:

我正在加快使用 ARC 进行 iOS 应用开发的速度。有时,我只需要一个普通的旧 c-struct 的普通 ole c-array 即可完成工作。在 ARC 之前,我只需将 free() 添加到我的 dealloc 方法中。有了 ARC,就不再需要 dealloc。我可以添加一个 ARC 指令来告诉编译器处理释放我的 c 数组吗?

Tom 的回答是 dealloc 方法

// EIVertex
struct EIVertex {
    GLKVector3 p;
    GLKVector3 n;
    GLKVector3 barycentric;
    GLKVector2 st;
};

typedef struct EIVertex EIVertex;

// ivar declaration
EIVertex *_vertices;

// malloc an array of EIVertex
_vertices = (EIVertex *)malloc([_triangles count] * sizeof(EIVertex));

// Note lack of [super dealloc]
- (void)dealloc{

    // ARC will not handle mem. management for plain ole c arrays.
    free(_vertices);
}

【问题讨论】:

  • EIVertex 是一个 Objective-C 类吗?你永远不应该在 Objective-C 类上使用sizeof,因为类的大小可以在运行时改变。也许你想要一个指针数组。
  • @newacct,EIVertex 是一个普通的 ole C 结构。我刚刚更新了代码 sn-p 以使其更清晰。很抱歉造成混乱。

标签: objective-c automatic-ref-counting dealloc


【解决方案1】:

你仍然可以重载dealloc。唯一的事情是你不能明确地调用它。所以像以前一样写dealloc,但不要在里面调用[super dealloc]

【讨论】:

  • @Tom,澄清一下你是说没有 ARC 指令吗?
  • @Tom,例如,如果我简单地将我的 ivar 声明为 __strong,ARC 会为我调用 free() 吗?
  • 没有。你必须在你的 dealloc 中对非objective-c 对象调用free。
猜你喜欢
  • 2012-04-01
  • 2012-01-11
  • 1970-01-01
  • 2013-07-10
  • 1970-01-01
  • 1970-01-01
  • 2012-03-21
  • 2012-03-13
  • 1970-01-01
相关资源
最近更新 更多