我通过 NSValue 将 CGMutablePathRefs 存储在 NSMutableArray 中。
像这样:
CGMutablePathRef path = CreateMutablePath();
[self.myArray addObject:[NSValue valueWithPointer:path]];
在我的 dealloc 中,我试过这个:
- (void)dealloc
{
[super dealloc];
for (int i = 0; i < self.myArray.count;) {
NSValue *currentPath = [self.myArray objectAtIndex:i];
CGMutablePathRef path = (CGMutablePathRef)[currentPath pointerValue];
CGPathRelease(path);
}
self.myArray = nil;
}
但是,当我运行它时,出现以下异常:
malloc: *** error for object 0x16b250: pointer being freed was not allocated
有人可以向我解释这是为什么以及我应该如何正确释放 CGMutablePathRefs 吗?
感谢任何帮助。
最佳答案
[super dealloc]
必须始终是 dealloc
的最后一行。
您永远不想以那种方式释放数组中的对象。让数组管理内存。
即:
CGMutablePathRef path = CreateMutablePath();
[self.myArray addObject:[NSValue valueWithPointer:path]];
CGPathRelease(path);
和:
- (void) dealloc
{
self.myArray = nil;
[super dealloc];
}
关于objective-c - 从 NSMutableArray 释放 CGMutablePathRef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6350917/