2010-09-06 51 views
0

在我的應用程序我有一個uiwebview我用來顯示圖像文件。現在的問題是我在這個視圖中泄漏了。這裏我寫了下面的代碼。在uiwebview分配內存泄漏

UIWebView *the_pWebView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; // the leak is on this line. 
the_pWebView.backgroundColor = [UIColor whiteColor]; 
the_pWebView.scalesPageToFit = YES; 
the_pWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); 
the_pWebView.delegate = self; 

self.m_pWebView = the_pWebView; 
[self.view addSubview: self.m_pWebView]; 
[the_pWebView release]; 

有人可以告訴我爲什麼代碼的第一行,我分配的uiwebview將泄漏。即使我已經發布了它?

回答

2

如果m_pWebView屬性是retaincopy那麼您需要確保您在該類的dealloc方法中釋放它。我懷疑你沒有在那裏發佈它。

+0

爲m_pWebView,我已經定義屬性(非原子的,保留),並且已經在dealloc方法中發佈了它。即使這個泄漏正在顯示。 – Jayshree 2010-09-06 09:32:19

0

你也應該加入它self.view

+0

我已經在dealloc方法中發佈了它。只要我將它添加到self.view中,我就無法發佈它。我也在其他地方使用它。 – Jayshree 2010-09-06 10:34:46

+0

將它添加到self.view後的保留計數是2,那麼應該釋放它,以便dealloc函數可以完全摧毀它! – Mayosse 2010-09-06 11:56:38

+0

更新:如果將self.m_pWebView添加到self.view,則不再對其釋放負責(如果在添加操作後將其釋放)。 self.view將銷燬它在dealloc上的子視圖。 – Mayosse 2010-09-06 12:27:36

0

在viewDidLoad中

[self performSelectorOnMainThread:@selector(abc) withObject:nil waitUntilDone:YES]; 

然後做一個函數名後釋放self.m_pWebView是ABC

-(void) abc 
    { 

UIWebView *the_pWebView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 

the_pWebView.backgroundColor = [UIColor whiteColor]; 

the_pWebView.scalesPageToFit = YES; 

the_pWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); 

the_pWebView.delegate = self; 

self.m_pWebView = the_pWebView; 


    [self.view addSubview: self.m_pWebView]; 

    [the_pWebView release]; 

iWebView=nil; 

}