我正在制作一个OpenGL应用程序,但是我收到了一条EXC_BAD_ACCESS消息。
这是我的代码:
//(draw function only, ask if you need more)
//objects: array of triangles/quads
//Triangle/Quad3D: should be obvious, I like OOP
//PS this is in a 'Composite Object' class
- (void)draw {
//NSLog(@"%@",objects);
NSMutableArray *quad, *tri;
quad = [NSMutableArray arrayWithCapacity:0];
tri = [NSMutableArray arrayWithCapacity:0];
NSEnumerator *e = [objects objectEnumerator];
id object;
while (object = [e nextObject]) {
if ([object isKindOfClass:[Triangle3D class]]) {
[tri addObject:(Triangle3D *)object];
} else if ([object isKindOfClass:[Quad3D class]]) {
[quad addObject:(Quad3D *)object];
}
}
NSLog(@"%d",[tri count]);
NSLog(@"%d",[quad count]);
[quad release];
[tri release];
[e release];
}
答案 0 :(得分:14)
您正在发布您不拥有的对象,因为它们是根据http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html
中给出的命名约定自动释放的如果使用名称以“alloc”或“new”开头或包含“copy”(例如,alloc,newObject或mutableCopy)的方法创建对象,或者如果发送它,则获取对象的所有权保留信息。
+arrayWithCapacity:
不以“alloc”或“new”开头,并且不包含“copy”,因此除非您明确保留该对象,否则不应释放它返回的对象。