When to properly release an object
I am trying to better understand when to properly rel开发者_Python百科ease an object in a fairly memory intensive program. My current doubt arises from the following bit of code:
- (void)scrollViewDidScroll:(UIScrollView *)localScrollView
{
InteractionPointModel *model = [[InteractionPointModel alloc] init];
for (int x=0; x<totalInteractionPoints; x++) {
model = [interactionPointData objectAtIndex:x];
CGPoint oldCenter = model->worldLocation;
CGPoint newCenter;
newCenter.x = oldCenter.x * [localScrollView zoomScale];
newCenter.y = oldCenter.y * [localScrollView zoomScale];
[[interactionPoints objectAtIndex:x] setCenter:newCenter];
}
[model release];
}
I would have thought that the program was done with model by now, but it crashes upon release. If I don't release it, the program runs, but obviously with a memory leak. What am I doing wrong?
The problem with your code is that you are leaking when you first enter on the loop.
InteractionPointModel *model = [[InteractionPointModel alloc] init];
The line above is allocating an object that won't be used.
model = [interactionPointData objectAtIndex:x];
The line above makes model
point to a different object, and so, the previous value is not pointed anymore.
[model release];
You get the crash when releasing because you are releasing a value that you don't own. When you get at release, if you enter on the loop, at least once, model
is pointing to the last object of the array interactionPointData
.
To fix your code, you need only to remove the wrong memory management.
- (void)scrollViewDidScroll:(UIScrollView *)localScrollView {
InteractionPointModel *model = nil;
for (int x=0; x<totalInteractionPoints; x++) {
model = [interactionPointData objectAtIndex:x];
CGPoint oldCenter = model->worldLocation;
CGPoint newCenter;
newCenter.x = oldCenter.x * [localScrollView zoomScale];
newCenter.y = oldCenter.y * [localScrollView zoomScale];
[[interactionPoints objectAtIndex:x] setCenter:newCenter];
}
}
You seem to be replacing a newly-allocated InteractionPointModel
object with an existing one in your interactionPointData
array. At the end of the method, after the loop, you're attempting to release what I suspect is an autoreleased object (coming from the array) as you say in your comment, an object from the array that you don't own, which causes the crash.
If you don't release it, the leak is caused by the newly-allocated object that you initialized at the beginning, which is no longer accessible because you've changed model
to point to objects in the array.
精彩评论