iphone - generic question about objects
I have several UIImagesView and I put them on a MutableArray. The MutableArray is retained. I add them also as a subview of a view.
After adding each UIImageViews on a view I release them... for example...
[myView addSubview:myImageView];
[myImageView release];
If I am not wrong, each imageView has now a retainCount of 2, because they are also st开发者_开发问答ored on the MutableArray.
Now I release the view, so, each imageView retainCount drops to 1.
At some point I do:
myMutableArray = nil;
is it OK to do that? As the imageViews have yet a retainCount of 1, I am not sure if putting the array to nil, the array which were storing the imageViews will release the images... I suppose so, but I would like to hear from you masters!
will this leak?
thanks
You should ignore retain counts because the system adds and removes retain count behind the scenes and you can't possibly track it.
Instead, you need to focus on matching the retains and releases pairs you yourself create.
In this case, the only thing you need to pay attention to is the possible retain of initializing the image views in the first place.
Usually it looks something like this:
UIImageView *imgView=[[UIImageView alloc] initWithImage....
[myMutableArray addObject:imgView];
[myView addSubview:imgView];
[imgView release];
And you're done. The array and view will manage their own retention and you matched the retain of your initialization with a release and all is right with the world.
Yes that will leak! You are setting myMutableArray to nil without releasing the array. Doing so will leak the memory from the NSMutableArray object AND all of the UIImageView objects that it contained. If you want to dispose of the array and its contents, then release the array just like you would release any other object:
[myMutableArray release];
myMutableArray = nil;
The array will then send a release message to each object that it contains.
精彩评论