在uiwebview分配内存泄漏

问题描述:

在我的应用程序我有一个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将泄漏。即使我已经发布了它?

如果m_pWebView属性是retaincopy那么您需要确保您在该类的dealloc方法中释放它。我怀疑你没有在那里发布它。

+0

为m_pWebView,我已经定义属性(非原子的,保留),并且已经在dealloc方法中发布了它。即使这个泄漏正在显示。 – Jayshree 2010-09-06 09:32:19

你也应该加入它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

在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; 

}