objective-c - 从 NSMutableArray 释放 CGMutablePathRef

标签 objective-c ios nsmutablearray

我通过 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/

相关文章:

ios - 如何将原始类型的 3D 数组包装在 NSArray 中?

iOS:webview 滑动手势

iOS Swift 更新数组中的字典

objective-c - iOS 13 TLS 问题

ios - 为什么我无法使用 ios 9 打开 facebook 和 twitter 应用程序

ios - SDWebImage-避免在进入背景时清除图像缓存

objective-c - 如何制作自定义对象的副本

ios - uicollectionview 在底部滚动加载更多数据。 iOS swift

objective-c - 使用摄像头检测心率

ios - 如何在 AFNetworking 3.0 中设置outputStream