使用Apple的PhotoScroller示例重新使用分配給視圖的內存,一旦保留計數達到0,我就無法釋放內存。這裏是我的代碼,以便更好地理解: 這篇文章摘自PhotoScroller
PhotoViewController.m從UIScrollView中移除視圖
- (void)configurePage:(ImageScrollView *)page forIndex:(NSUInteger)index
{
page.index = index;
page.frame = [self frameForPageAtIndex:index];
[page displayPage:index];
}
ImageScrollView.h
@interface ImageScrollView : UIView
{
UIViewController *vc;
NSUInteger index;
}
@property (assign) NSUInteger index;
- (void)displayPage:(int)indice;
@end
ImageScrollView.m
- (void)dealloc
{
[vc release];
[super dealloc];
}
- (void)displayPage:(int)indice
{
//remove previous view
[vc.view removeFromSuperview];
[vc release];
vc = nil;
//NSLog(@"vc retain %i", [vc retainCount]);
// make a new viewController for the new page
Class clss = NSClassFromString([NSString stringWithFormat:@"page_0%i", indice + 1]);
vc = [[clss alloc] initWithNibName:[NSString stringWithFormat:@"page_0%i", indice + 1] bundle:nil];
[self addSubview:vc.view];
}
的類 「page_0x」 是UIViewControllers。到目前爲止,XIB上只有一個UIImageView。 一個page_01的例子:
@implementation page_01
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
存儲器峯到裝置148 MB。給我一條內存警告級別1,然後釋放一些內存。下降到95 MB。繼續上下。
1. dealloc正在被調用。 2.我的page_0x viewcontrollers上沒有任何內容。到目前爲止,我只是在向它扔一個png。 3.構建和分析不會給我任何奇怪的東西。 4.沒有泄漏。 – GianPac 2010-09-22 12:43:14
否NSZombieEnabled。但我注意到,當我得到一個內存警告級別1時,一些內存被取消分配。它從148MB到95MB。然後它再次增加,直到它遇到另一個記憶警告。我現在的問題是,如果每次對象的保留計數爲0時我應該排空游泳池。 – GianPac 2010-09-22 15:13:10
設備上沒有泄漏。如果我用png刪除UIImageView,似乎沒有任何改變。它似乎很尷尬,它等待內存警告實際上釋放內存 – GianPac 2010-09-22 17:09:49