this is my first iOS App, i follow apple documentation and stackoverflow answers, in xcode my app is working as charm on both simulator and device, but on instrument i have a memory leak after 50 - 70 seconds from app launch.
i use tab controller with 3 view controllers,3 webviews, each webview is showing a web page like this:
<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"> <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>
<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"> <?=unFormatData($mydata[5-$minusIndex])?><br>
<img border="0" src="images/web.png" width="30" height="30" align="absmiddle"> <?=unFormatData($mydatad[4])?></span>
<br><br><?=unFormatData($mydatad[2])?>
<p style="padding-right:10px;line-height:200%" >
</p><br>
</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> </td></tr>
</table>
using xcode > app is running perfect on simulater and device with no errors at all.
using instrument > if i didn't do any action inside webview, no memory leak at all, when i begin clicking and moving inside the web page, after around a minute i receive a memory leak.
in the beginning, i thought its because i run 3 webviews on same time, but when i make a test on 1 webview, same problem occurred.
this is my code:
#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
where is the problem ? my second question and maybe it will help to resolve first problem, how to make the tabbar buttons, load the webview from url request on second time click, to explain more: when i first click a tabbar button1, it loads a view controller1 which loads a webview1 which loads a url request, click another tabbar button2 then go back to tabbar button1, it doesn't load the webview1 again, it only show it as you left it,because tabbar related to viewcontroller not to webview. how to fix it.
thanx in advance for any help
UPDATE: @Alexander here is sample code from main window before second section:
- (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];
}
I'm not so sure about this leak in webview since you didn't provide the way you create them but you definitely have to use [super loadView] in your loadView class and you do not release your tabBarController. I'm not so sure what did you used before SecondSection class. What about the webView loading you should make webView your class variable and then use the event comes in UIViewController method -(void)viewWillAppear:(BOOL)animated. Don't forget about super.
Macaesar try this:
1) Make your viewcontroller delegate of your UIWebView.
2) Add the following method:
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
}
It resolved my leak.
精彩评论