2011-08-25 56 views
1

我一直卡在我的iPhone應用程序的內存泄漏問題現在很好。我覺得我必須錯誤地閱讀我的數據。似乎每當我分配內存的時候,有太多開銷導致泄漏,當我釋放數據時,內存使用量幾乎沒有下降或根本沒有下降。一個已經浪費了2天是我的flipside視圖控制器上的UIWebview加載一個url,我的應用程序的內存使用從3 MB跳到7.我在我的dealloc方法中釋放webview,但巨大的內存塊仍然存在。有沒有人有任何建議。iPhone荒謬的內存泄漏

- (void)viewDidLoad { 
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor]; 

nav_bar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width+20,45)]; 
[self.view addSubview:nav_bar]; 
[UINavigationBar release]; 

rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done)]; 
item = [[UINavigationItem alloc] initWithTitle:@"Flipside View"]; 
item.rightBarButtonItem = rightButton; 
item.hidesBackButton = YES; 
[nav_bar pushNavigationItem:item animated:NO]; 
[rightButton release]; 
[item release]; 

NSAutoreleasePool *initPool = [[NSAutoreleasePool alloc] init]; 

web_view = [[UIWebView alloc]initWithFrame:CGRectMake(0,45,self.view.frame.size.width,self.view.frame.size.height - 45)]; 

web_view.autoresizesSubviews = YES; 
web_view.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth); 

NSString *urlAddress = @"http://www.tutorialpark.com/wpcontent/uploads/3/HeartBlending.jpg"; 
NSURL *url = [NSURL URLWithString:urlAddress]; 
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 

[web_view loadRequest:requestObj]; 
[self.view addSubview:web_view]; 
[web_view release]; 
[initPool release]; 

[super viewDidLoad]; 
} 

- (void)dealloc { 
    [nav_bar removeFromSuperview]; 
    [web_view removeFromSuperview]; 
    [rightButton release]; 
    [super dealloc]; 
} 

我對縮進表示歉意我非常生氣,不想處理它。

+0

順便說一句我看到這些泄漏在模擬器和真實設備 – Daniel

回答

1

[UINavigationBar release]; - 這是幹什麼的?它是否意味着[nav_bar發佈]? - 如果是這樣的話,稍後應該稍微再往下訪問nav_bar。但那麼它似乎是一個成員變量?所以它應該在dealloc中發佈。

rightButton正在釋放兩次 - 一次在viewDidLoad中,一次在dealloc中。

你能解釋一下自動發佈池的用途嗎?

+0

autorelease池是從舊的代碼我必須掃視了它,當我張貼遺留下來的兩個 – Daniel

2

您似乎對引用計數的工作方式感到困惑。下面

看評論:

- (void)viewDidLoad { 
    self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor]; 

    UINavigationBar* nav_bar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width+20,45)]; 
    // nav_bar retaincount 1 
    [self.view addSubview:nav_bar]; 
    // nav_bar retaincount 2 
    [nav_bar release]; 
    // nav_bar retaincount 1 - now controlled by self.view 

    UIBarButtonItem* rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(done)]; 
    // rightButton retaincount 1 

    UINavigationItem* item = [[UINavigationItem alloc] initWithTitle:@"Flipside View"]; 
    // item retaincount 1 

    item.rightBarButtonItem = rightButton; 
    // rightButton retaincount 2 

    [rightButton release]; 
    // rightButton retaincount 1 - now controlled by item 

    item.hidesBackButton = YES; 
    [nav_bar pushNavigationItem:item animated:NO]; 
    // item retaincount 2 

    [item release]; 
    // item retaincount 1 - now controlled by nav_bar 

    UIWebView* web_view = [[UIWebView alloc]initWithFrame:CGRectMake(0,45,self.view.frame.size.width,self.view.frame.size.height - 45)]; 
    // web_view retaincount 1 

    web_view.autoresizesSubviews = YES; 
    web_view.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth); 

    NSString *urlAddress = @"http://www.tutorialpark.com/wpcontent/uploads/3/HeartBlending.jpg"; 

    NSURL *url = [NSURL URLWithString:urlAddress]; 
    // url is autoreleased, you can ignore.. 

    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
    // requestObj is autoreleased, you can ignore.. 

    [web_view loadRequest:requestObj]; 

    [self.view addSubview:web_view]; 
    // web_view retaincount 2 

    [web_view release]; 
    // web_view retaincount 1 - now controlled by self.view 

    [super viewDidLoad]; 
} 

- (void)dealloc { 
    // don't need to do anything because all the memory is controlled by self.view, will be released when the internal variable self.view is released. 
    [super dealloc]; 
} 
+0

感謝我明確地澄清了一些事情,但在我解僱filpside視圖後,webview加載的數據仍然存在。它很好,因爲它不必重新加載url,但我希望它不再被緩存在內存中。有沒有一種方法來釋放加載到webview中的數據,我認爲這將通過webview釋放來完成,但根據我的方便的分配圖,它不是。 – Daniel

+0

最好把保留計數看作一個三角形;您添加和減去保留計數。在上面,評論很可能是不正確的 - 保留計數可能是2,可能是5,可能是492,重要的是*它並不重要*。你應該*只擔心你留下的保留。 – bbum

+0

的確,計數可能是任何事情,但它更多地意味着對計數的改變以及大致轉移負債發生的位置進行粗略的心理指導。 –