I have as property in a view, an array of array like this:
@interface MyView : UIView
@property (nonatomic) CGPoint **matrix;
@end
in the controller that own this view I have load the data in the -viewDidLoad and free memory in the -viewDidUnload like this:
- (void)viewDidLoad
{
self.myView.matrix = malloc(sizeof(CGPoint*) * array1Size);
for (int k = 0; k < array1Size; k++) {
self.myView.matrix[k] = malloc(sizeof(CGPoint) * innerArraySize);
}
}
- (void)viewDidUnload
{
for (int k = 0; k < array1Size; k++) {
free(self.myView.matrix[k]);
self.myView.matrix[k] = nil;
}
free(self.myView.matrix);
self.myView.matrix = nil;
[self setMyView:nil];
[super viewDidUnload];
}
While profiling I see a leak here. Can someone help me where I’m wrong?
thanks
update:
i try to remove free code from viewDidUnload and use dealloc like this:
-(void)dealloc {
[self freeArray];
}
- (void) freeArray {
for (int k = 0; k < 5; k++) {
free(self.myView.matrix[k]);
self.myView.matrix[k] = NULL;
}
free(self.myView.matrix);
self.myView.matrix = NULL;
}
then I embed init code in:
if (self.graphView.matrix == NULL) {
...
}
now no more leak, THANKS!
Your code does not make sure that each setup of the matrix is matched by exactly one tear down. For example,
viewDidUnloadis not guaranteed to be called. Also, you have no guards against duplicate setup or tear down.If you really need the C array of arrays, a better approach would be to create it in the initializer of the view (
initWithFrame:orinitWithCoder:) and remove it in itsdealloc.Edit: To alleviate your concerns regarding
deallocand ARC:You can certainly override dealloc in ARC and rely on it being called. The only difference is that you cannot explicitly call the overridden implementation (
[super dealloc]). ARC will insert that for you.