I am storing CGMutablePathRefs inside of a NSMutableArray via NSValue.
Like so:
CGMutablePathRef path = CreateMutablePath();
[self.myArray addObject:[NSValue valueWithPointer:path]];
In my dealloc, I have tried this:
- (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;
}
However, when I run this, I get the following exception:
malloc: *** error for object 0x16b250: pointer being freed was not allocated
Can someone please explain to me why this is and how I should correctly release the CGMutablePathRefs?
Any help is appreciated.
[super dealloc]must always be the last line of yourdealloc.You do not ever want to release objects in an array in that fashion. Let the array manage the memory.
I.e.:
And: