仅使用仪器一分钟后,webview中的内存泄漏

时间:2011-07-12 12:29:09

标签: iphone ios memory-leaks webview

这是我的第一个iOS应用程序,我遵循苹果文档和stackoverflow答案,在xcode中我的应用程序在模拟器和设备上都起到了魅力,但在仪器上我在应用程序启动后50到70秒内有内存泄漏。 / p>

我使用带有3个视图控制器的标签控制器,3个webview,每个webview显示如下网页:

<html dir="rtl">
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="newstyle.css" type="text/css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/iphone.js"></script>
<title></title>
</head>
<body style="color:#FFFFFF">
<div align="center">
<table border="0" width="320" id="table1" cellspacing="5">

<tr>
<form method="get">
<input type="hidden" name="index" value="<?=$_GET['index']?>">
<input type="hidden" name="cat" value="<?=$_GET['cat']?>">
<input type="hidden" name="area" value="<?=$_GET['area']?>">
<td class="search">
<div class="back"><a href="javascript:window.history.back()">back</a></div>
    <div class="srchbox"><a href="#reviews">&nbsp;<sub>notes (<?=countDataPages('iresturant_reviews','status=1 and clientIIndex ='.$_GET['index'].' and clientID='.$_GET['id'], 1);?>)</sub></a><div id="rates"><div id="actualrates" style="width:<?=round($mydatad[7] * 10)?>%"></div></div>
</td>
</form>
</tr>
    <tr>
    <td style="padding-left: 10px; padding-right: 10px" width="265">
<div class="justBox" style="padding:10px;"> 
<b><?=unFormatData($mydata[1])?></b><br>
    <span style="font-weight: 400"><?=$catTitle[$mydata[3-$minusIndex]]?>
    <br><br>
    <?=$areaTitle[$mydata[4-$minusIndex]]?><br>
    <?=unFormatData($mydatad[3])?>
    </span>
        <p align="left" style="margin:0px;margin-top:15px;">
    <a href="page1.php?id=<?=$mydata[0]?>&index=<?=$_GET['index']?>"><img border="0" src="images/zx.png" width="40" height="40"></a>&nbsp;&nbsp;
        <a href="page2.php?id=<?=$mydata[0]?>&index=<?=$_GET['index']?>"><img border="0" src="images/xz.png" width="40" height="40"></a>
        </div></td>
    </tr>

    <tr>
    <td style="padding-left: 10px; padding-right: 10px">    
    <?
    $imgFile = 'uimages/index_'.$_GET['index'].'_'.$mydata[0].'.jpg';
    if (file_exists($imgFile))
    {
    ?>

        <img src="<?=$imgFile?>" width="100%" align="right" style="margin-left:30px;"><p>
    <?
    }
    ?></td>
    </tr>
            <tr>
    <td style="padding-left: 10px; padding-right: 10px"><br><br>
        <img border="0" src="images/phone.png" width="30" height="30" align="absmiddle"> &nbsp; <?=unFormatData($mydata[5-$minusIndex])?><br>
        <img border="0" src="images/web.png" width="30" height="30" align="absmiddle"> &nbsp; <?=unFormatData($mydatad[4])?></span>
    <br><br><?=unFormatData($mydatad[2])?>
    <p style="padding-right:10px;line-height:200%"  >
    </p><br>&nbsp;
</td>
    </tr>
<tr>
<td class="search">
    <div class="back"><a href="javascript:window.history.back()">back</a></div>
    <p style="margin-right: 10px; margin-top: 5px">
<a name="reviews"></a><font color="#FFFFFF">reviews</font>
</td>
</tr>       
<tr>
<td style="padding-left: 10px; padding-right: 10px" >
<?
    $data = getAllData('iphone_reviews','status=1 and clientIIndex ='.$_GET['index'].' and clientID='.$_GET['id'].' order by date desc');
for ($i=0; $i < sizeof($data); $i++)
{
?>  
<div class="justBox" style="padding:10px;"> 
<p style="margin:5px;"><?=unFormatData($data[$i][3])?></p>
<p dir="ltr" style="margin:0px;"><?=date("F j, Y h:i A", $data[$i][7])?></p>
<p style="margin:0px;font-weight: 400; padding:10px;"><?=unFormatData($data[$i][5])?></p>
</div>
<hr size="1">
<?
}
?>

</td>
</tr>   
<tr>
<td class="search">
    <div class="back"><a href="javascript:window.history.back()">back</a></div>
    <p style="margin-right: 10px; margin-top: 5px">
<font color="#FFFFFF">review</font>
</td>
</tr>
<tr><td style="padding-left: 10px; padding-right: 10px">
<div id="addFrm">
    <br>
    name<br>
    <input type="text" id="name" size="20" style="width:100%"><br>
    <br>
    review<br>
    <textarea rows="2" id="comment" cols="20"  style="width:100%; height:71"></textarea><br>
    <br>
    rate <?=$oTitle?><select size="1" id="rate">
    <?
    for ($i=10; $i > 0; $i--)
    {
    echo '<option value="'.$i.'">'.$i.'</option>';
    }
    ?>
    </select></span>
<span lang="ar-sy">from 10<br>
    <br>
    <button type="button" class="submitbutton" onclick="submitComment()">send</button></span></div></td></tr>   
<tr><td>&nbsp;</td></tr>    
</table>

使用xcode&gt;应用程序在模拟器和设备上运行完美,完全没有错误。

使用仪器&gt; 如果我没有在webview中做任何动作,根本没有内存泄漏,当我开始点击并在网页内移动时,大约一分钟后我收到内存泄漏。

一开始,我认为是因为我同时运行了3个网页浏览量,但是当我在1个网页浏览上进行测试时,发生了同样的问题。

这是我的代码:

#import "SecondSection.h"
#import "NamesViewController.h"
#import "TypesViewController.h"
#import "AreasViewController.h"


@implementation SecondSection


- (void)loadView {
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bk.png"]];
self.view = contentView;
[contentView release];

// Declare all three view controllers
NamesViewController *NamesInfoController = [[[NamesViewController alloc] initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]] autorelease];

TypesViewController *typesViewController = [[[TypesViewController alloc] init] autorelease];
AreasViewController *areasViewController = [[[AreasViewController alloc] init] autorelease];

// Set a title for each view controller. These will also be names of each tab
NamesInfoController.title = @"names";
typesViewController.title = @"types";
areasViewController.title = @"areas";

NamesInfoController.tabBarItem.image = [UIImage imageNamed:@"names.png"];
typesViewController.tabBarItem.image = [UIImage imageNamed:@"types.png"];
areasViewController.tabBarItem.image = [UIImage imageNamed:@"areas.png"];


// Create an empty tab controller and set it to fill the screen minus the top title bar
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.view.frame = CGRectMake(0, 0, 320, 460);

// Set each tab to show an appropriate view controller
[tabBarController setViewControllers:
 [NSArray arrayWithObjects:NamesInfoController, typesViewController, areasViewController, nil]];

// Clean up objects we don't need anymore
[NamesInfoController release];
[typesViewController release];
[areasViewController release];

// Finally, add the tab controller view to the parent view
[self.view addSubview:tabBarController.view];    
}

- (void)dealloc
{
[super dealloc];
}

- (void)viewWillDisappear
{

}


- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];
}

- (void)viewDidUnload
{
[super viewDidUnload];

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
问题在哪里? 我的第二个问题,也许它将有助于解决第一个问题,如何制作标签栏按钮,在第二次点击时从网址请求加载webview,以解释更多:当我第一次点击标签栏button1时,它加载一个视图controller1加载加载网址请求的webview1,单击另一个标签栏按钮2然后返回标签栏button1,它不会再次加载webview1,它只会在你离开时显示它,因为tabbar与viewcontroller相关而不是webview。如何解决它。

提前获取任何帮助

更新:@Alexander这里是第二部分之前主窗口的示例代码:

- (IBAction)rests:(id)sender;
{
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = @"Loading";
[HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];
SecondSection *zcontroller = [[SecondSection alloc] init];
zcontroller.title = @"macaesar";
[[self navigationController] pushViewController:zcontroller animated:YES];
[zcontroller release];
}

2 个答案:

答案 0 :(得分:0)

我对webview中的这个漏洞不太确定,因为你没有提供创建它们的方式,但是你必须在loadView类中使用[super loadView]并且不释放tabBarController。我不太确定你在SecondSection课之前用过什么。那么webView加载你应该把webView变成你的类变量然后使用UIViewController方法中的事件 - (void)viewWillAppear:(BOOL)动画。不要忘记超级。

答案 1 :(得分:0)

Macaesar试试这个:

1)创建UIWebView的viewcontroller委托。

2)添加以下方法:

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
}

它解决了我的泄漏问题。