【发布时间】: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