PDA

View Full Version : getting errors and freezing when deploying UIWebView with youtube page..




primedime
Apr 17, 2011, 01:25 PM
I have two applications in which I use a UIwebView in order to show the mobile youtube page.... I am not sure how to really explain the actual error because this is the first time I am seeing this type of problem... The thing is that it only happens if I try to open that webview multiple times in one session. If I just open it one time it works fine, the second I launch it again it freezes and crashes.... it says Thread1: Program received signal "EXC_BAD_Access".

Here is the code, any help is much appreciated:


#import "YoutubeViewController.h"
#import "MainViewController.h"

@implementation YoutubeViewController

@synthesize myWebView;
@synthesize toolbar;

- (void)loadWebPageWithString:(NSString *)urlString
{
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:request];
}

-(IBAction)back
{
[self.parentViewController dismissModalViewControllerAnimated:YES];
}

-(IBAction)goHome
{
NSString *urlAddress = @"http://m.youtube.com/primedime21";

NSURL *address = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:address];
[myWebView loadRequest:requestObj];
}

-(IBAction)reload
{
NSLog(@"reload");
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewWillDisappear:(BOOL)animated
{
[myWebView stopLoading];
}

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

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

// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];

[self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.youtube.com/primedime21"]]];
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

return YES;
}

@end



nickculbertson
Apr 17, 2011, 01:54 PM
I have had the same problem before with putting youtube user pages in an app. I think the only solution is to work around the problem... embed the videos you want to play or have the app navigate to the youtube app for playback.

Nick

dejo
Apr 17, 2011, 01:59 PM
EXC_BAD_ACCESS is caused by over-released or under-retained objects. You should probably post the code where you present your YoutubeViewController, since that's where your issue seems to be popping up.

primedime
Apr 17, 2011, 02:02 PM
Here is my main view:


#import "MainViewController.h"

@implementation MainViewController

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
*/

-(IBAction)switchView{

YoutubeViewController *youtube = [[YoutubeViewController alloc]
initWithNibName:@"YoutubeViewController" bundle:nil];
youtube.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:youtube animated:YES];
[youtube release];

FacebookViewController *facebook = [[FacebookViewController alloc]
initWithNibName:@"FacebookViewController" bundle:nil];
facebook.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:facebook animated:YES];
[facebook release];

}


// set up email button
- (IBAction)email:(id)sender;
{
MFMailComposeViewController *mailComposer =
[[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;

if ([MFMailComposeViewController canSendMail]) {
[mailComposer setToRecipients:[NSArray
arrayWithObjects:@"rjossfolk@gmail.com", nil]];
[mailComposer setSubject:@"Inquery from mobile app"];
[mailComposer setMessageBody:nil isHTML:NO];
[self presentModalViewController:mailComposer animated:YES];
}
}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self dismissModalViewControllerAnimated:YES];

if (result == MFMailComposeResultFailed) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Unable to send mail"
message:@"Please,check your internet connection and try again"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}


//Flip side
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
[self dismissModalViewControllerAnimated:YES];
}

- (IBAction)showInfo:(id)sender
{
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
controller.delegate = self;

controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];

[controller release];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations.
return YES;
}

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

// Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload
{
[super viewDidUnload];

// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

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

@end


Here is the YoutubeViewController:


#import "YoutubeViewController.h"
#import "MainViewController.h"

@implementation YoutubeViewController

@synthesize myWebView;
@synthesize toolbar;

- (void)loadWebPageWithString:(NSString *)urlString
{
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:request];
}

-(IBAction)back
{
[self.parentViewController dismissModalViewControllerAnimated:YES];
}

-(IBAction)goHome
{
NSString *urlAddress = @"http://m.youtube.com/primedime21";

NSURL *address = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:address];
[myWebView loadRequest:requestObj];
}

-(IBAction)reload
{
NSLog(@"reload");
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewWillDisappear:(BOOL)animated
{
[myWebView stopLoading];
}

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

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

// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];

[self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.youtube.com/primedime21"]]];
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

return YES;
}

@end



Let me know if you need to see anything else.... Thanks a lot!

dejo
Apr 17, 2011, 02:06 PM
Explain switchView. Why are you presenting a YoutubeViewController and a FacebookViewController?

primedime
Apr 17, 2011, 02:15 PM
If you take a look at that image I have 4 buttons at the bottom not including the info button. for the youtube, facebook and linkedin I want to open them in a UIWebView and the Twitter button will open a twitter feed I have made in my previous application.... The switchView is the event I am using to switch from the main view to the views for youtube and facebook... I think i am missing an IF statement in there which I am looking up currently but none the less my youtubeviewcontroller crashes if I open it more than once in a session.... Am I clear enough in this description?


http://farm6.static.flickr.com/5267/5628629098_b342677e25.jpg (http://www.flickr.com/photos/rjossfolk/5628629098/)
Screen shot 2011-04-17 at 12.13.38 PM (http://www.flickr.com/photos/rjossfolk/5628629098/) by primedime (http://www.flickr.com/people/rjossfolk/), on Flickr

chown33
Apr 17, 2011, 02:30 PM
Have you run your code using Instruments, with zombies enabled?

That should almost always be the first step when getting this kind of error.

primedime
Apr 17, 2011, 02:34 PM
To be totally honest with you I have never used instruments before, I am pretty new to xcode and still learning. I have tried to open it before but didn't know what to do next.... I have troubles reading the resources as well since I have never done any coding before I started a few months ago but I am getting better at understanding the terminology.... I plan on looking in to instruments though because I have an app ready for the store but when I put it on my phone it has a few crashes here and there.... This is my second app now so hopefully soon I can use instruments...

dejo
Apr 17, 2011, 02:37 PM
Also, might want to try Build and Analyze (Xcode 3) or Project > Analyze (Xcode 4).

primedime
Apr 17, 2011, 02:50 PM
Ok, I used the Analyze thing you mentioned and the only thing that came up is this:

http://farm6.static.flickr.com/5149/5628165985_bb3e670fd1.jpg (http://www.flickr.com/photos/rjossfolk/5628165985/)
Screen shot 2011-04-17 at 3.46.44 PM (http://www.flickr.com/photos/rjossfolk/5628165985/) by primedime (http://www.flickr.com/people/rjossfolk/), on Flickr


This has to do with the mail button I have but that is working perfectly fine on the simulator and on my iphone....

chown33
Apr 17, 2011, 03:13 PM
To be totally honest with you I have never used instruments before, I am pretty new to xcode and still learning. I have tried to open it before but didn't know what to do next.... I have troubles reading the resources as well since I have never done any coding before I started a few months ago but I am getting better at understanding the terminology.... I plan on looking in to instruments though because I have an app ready for the store but when I put it on my phone it has a few crashes here and there.... This is my second app now so hopefully soon I can use instruments...

It's not overly difficult.
http://meandmark.com/blog/2010/09/using-nszombie-with-instruments/

http://developer.apple.com/library/ios/#documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/AboutTracing/AboutTracing.html

primedime
Apr 17, 2011, 03:22 PM
Cool, I am going to run it right now and see what's up... I already added all the other views and attached them to all the other buttons... The only view giving me trouble is the YoutubeViewController.....

PhoneyDeveloper
Apr 17, 2011, 03:35 PM
The error shown by Build and Analyze is correct. Your code shows a leak of the mailcomposer. There should be a release for that view controller at the end of the method. However that memory leak shouldn't cause a crash.

What line in your code is causing the bad access crash?

nickculbertson
Apr 17, 2011, 04:30 PM
Try changing the url to anything other than youtube and you will see that everything is working fine. The Youtube user page is your problem. Do a google search of your topic and you will find that this a common issue with youtube. You can however have the youtube button open up the youtube app to play videos from there. http://forums.macrumors.com/showthread.php?t=1093715&highlight=

primedime
Apr 17, 2011, 05:47 PM
The error shown by Build and Analyze is correct. Your code shows a leak of the mailcomposer. There should be a release for that view controller at the end of the method. However that memory leak shouldn't cause a crash.

What line in your code is causing the bad access crash?

I added [mailComposer release]; to it and it still shows a leak. Any ideas on what exactly I am missing?

And the error is in the main.m file


#import <UIKit/UIKit.h>

int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil); <-- here is the error
[pool release];
return retVal;
}



Also, how would I go about having it open the youtube app to my youtube channel? Thanks guys!

dejo
Apr 17, 2011, 07:54 PM
I added [mailComposer release]; to it and it still shows a leak.
Added it to what? Where?

nickculbertson
Apr 17, 2011, 08:54 PM
perhaps you could load the youtube page in the mainview's viewdidload method and make it hidden until the youtube button is pushed (without opening a second xib). This way the page is only loaded once with the viewdid of your mainview. I've never tried it but its worth a shot.

Nick