开发者

When should I deallocate memory, if all?

I’ve read all the iOS memory allocation/deallocation basics, but can’t find anything on the following:

I’ve created a small app, that pretty much lists a grid of UIButtons and clicking on any of those, it adds a UIScrollView to the current controller view (and adds a bunch of UIView and UIWebView’s inside etc).

On adding the UIScrollVIew, I also add a UIButton, that takes me back to the "home grid" and th开发者_如何学Goen removes the UIScrollView from the superview. I release all the things I’m retaining/allocating etc and when I check the app with Instruments, it doesn’t show any memory leak.

But every time I tap on any of the UIButton objects, I allocate more memory (according to Instruments) and it keeps growing – and "reopening" the same kind of UIScrollView from a button always adds more memory allocations.

If I simulate a memory warning in the simulator, it deallocated a bit of the memory and I can then keep growing it again.

So here’s my question: should I bother with trying to deallocate this somehow manually? And if so, where should I actually do this? I’m quite new to Obj-C, so I think I have most of the basics covered, but more advanced topics still require some help.

Creating the grid:

UIScrollView *grid = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
[[self view] addSubview:grid];
[grid release];

Adding buttons (in a for loop):

UIButton *slotItem = [[UIButton alloc] initWithFrame:CGRectMake(((float) slotWidth * j), ((float) slotHeight * i), (float) slotWidth, (float)slotHeight)];
[grid addSubview:slotItem];

UIWebView *buttonWebThumb = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, slotWidth, slotHeight)];

[buttonWebThumb setBackgroundColor:[UIColor clearColor]];
[buttonWebThumb setOpaque:NO];

[buttonWebThumb loadHTMLString:[NSString stringWithFormat:@"%@ %@ %@", htmlTop, [[row objectAtIndex:j] objectAtIndex:1], htmlBottom] baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
buttonWebThumb.scalesPageToFit = YES;
[slotItem addSubview:buttonWebThumb];
buttonWebThumb.userInteractionEnabled = NO;
buttonWebThumb.exclusiveTouch = NO;
[buttonWebThumb release];

[slotItem addTarget:self action:@selector(showPages:) forControlEvents:UIControlEventTouchUpInside];

[slotItem release];

showPages method then creates another UIScrollView and adds 1-10 separate UIWebView’s in there and adds a “close” button to the new UIScrollView.


You're probably seeing this because you aren't removing all elements from the view before leaving it.

For example, if you exit the view with a method like this, do this:

- (id) viewExitAction: (id) sender {
  id elem;
  for(elem in buttons) {
    [elem removeSelfFromView];
  }
  // etc
}

This will automatically decrease the retain counts of these objects down to zero. (Provided you added them to the view and then released them, as you're supposed to.)

Otherwise, the viewDidUnload and dealloc methods never get called.


Dealloc is invoked indirectly through the release method, and you should never call it directly.NSObject Manual on dealloc

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜