我读过的所有iOS的内存分配/释放的基础知识,但找不到任何东西如下:何时应该释放内存,如果全部?
我创建了一个小的应用程序,这非常列表UIButtons的网格,点击任何的那些,它将UIScrollView添加到当前控制器视图(并添加了一堆UIView和UIWebView的内部等)。
在添加UIScrollVIew时,我还添加了一个UIButton,它将我带回“家庭网格”,然后从超级视图中移除UIScrollView。 我释放了所有我保留/分配的东西,当我用Instruments检查应用程序时,它不显示任何内存泄漏。我每次点击任何UIButton对象时,都会分配更多内存(根据Instruments),并且它不断增长 - 并且从一个按钮“重新打开”相同类型的UIScrollView总是会增加更多的内存分配。
如果我在模拟器中模拟内存警告,它会释放一部分内存,然后我可以继续增长它。
所以这里是我的问题:我应该打算试图以某种方式手动释放这个?如果是这样,我应该在哪里做到这一点?我对Obj-C颇为陌生,因此我认为我涵盖了大部分基础知识,但更高级的主题仍然需要一些帮助。
创建网格:
UIScrollView *grid = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
[[self view] addSubview:grid];
[grid release];
添加按钮(在for循环中):
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方法接着创建另一个的UIScrollView,并增加了1-10单独的UIWebView在那里,并增加了一个“关闭“按钮到新的UIScrollView。
如果您显示一些代码,这将更容易回答。但是,您不希望自己释放任何对象,当保留计数达到零时,这会自动发生。如果没有发生,某个对象仍然会引用它。 – omz 2011-05-22 22:05:15