Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-10-2010, 03:56 PM   #1 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 34
nightfox500 is on a distinguished road
Smile [FIXED] NSURLRequest with EXC_BAD error

Hello,

I'm trying to load my UITable images async but when I try that I get an EXC_BAD_ACCESS error and I don't know exactly why!

If anybody has an suggestion/improvement please make!

ProductListViewController.m
-----------------------------------------------
Code:
//Here i call te new function
CGRect frame;
	frame.size.width=75; frame.size.height=75;
	frame.origin.x=0; frame.origin.y=0;
	AsyncImageView* asyncImage = [[[AsyncImageView alloc]
								   initWithFrame:frame] autorelease];
	asyncImage.tag = 999;
	NSURL* url = [NSString stringWithFormat:@"http://www.cameraland.nl/components/com_virtuemart/shop_image/product/thumbs/%@", [object valueForKey:@"ImageURL"]];
	[asyncImage loadImageFromURL:url];
	
	[cell.contentView addSubview:asyncImage];

AsyncImageView.m
-----------------------------------------------
Code:
- (void)loadImageFromURL:(NSURL*)url {
        //this if-loop should serve as an iOS 4 bug-fix
	if (![NSThread isMainThread]) {
		[self performSelectorOnMainThread:@selector(loadImageFromURL:)
							   withObject:url waitUntilDone:NO];
		return;
	}
	
	if (connection!=nil) { [connection release]; } //in case we are downloading a 2nd image
	if (data!=nil) { [data release]; }
	
	NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 30.0];
	
	NSURLResponse *response;
	NSError *error;
	NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
	if(error) {
		NSLog(@"error: %@", [error localizedDescription]);
	}
	
	connection = [[NSURLConnection alloc] initWithRequest: request delegate: self startImmediately: NO];
	[connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
	[connection start];
}
Regards,

Len

Last edited by nightfox500; 08-11-2010 at 05:34 AM.
nightfox500 is offline   Reply With Quote
Old 08-10-2010, 04:13 PM   #2 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: Switzerland
Age: 26
Posts: 35
Shod is on a distinguished road
Default

It would be nice to know at what line you get the bad access.

But I can see that your doing

Code:
NSURL* url = [NSString stringWithFormat:@"http://www.cameraland.nl/components/com_virtuemart/shop_image/product/thumbs/%@", [object valueForKey:@"ImageURL"]];
Here you are creating a NSString and assigning it to a NSURL variable, and that can't be good... maybe you wanted to do something like this:

Code:
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.cameraland.nl/components/com_virtuemart/shop_image/product/thumbs/%@", [object valueForKey:@"ImageURL"]]];
and why in your loadImageFromURL: method you are sending the request first synchronously and then again asynchronously?

Last edited by Shod; 08-10-2010 at 04:18 PM.
Shod is offline   Reply With Quote
Old 08-10-2010, 04:20 PM   #3 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 34
nightfox500 is on a distinguished road
Default

Quote:
Originally Posted by Shod View Post
It would be nice to know at what line you get the bad access.

But I can see that your doing

Code:
NSURL* url = [NSString stringWithFormat:@"http://www.cameraland.nl/components/com_virtuemart/shop_image/product/thumbs/%@", [object valueForKey:@"ImageURL"]];
Here you are creating a NSString and assigning it to a NSURL variable, and that can't be good... maybe you wanted to do something like this:

Code:
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.cameraland.nl/components/com_virtuemart/shop_image/product/thumbs/%@", [object valueForKey:@"ImageURL"]]];
Good point, sadly this is not the solution. I still get the EXC_BAD_ACCESS error. As far as I can find out the EXC_BAD_ACCESS occours at that line and then proceeds to the calling part in AsyncImageView.m. And here a screenshot from my debugger:


Here is a little piece of the backtrace information.
Code:
#0  0x028aea93 in objc_msgSend ()
#1  0x001e77b0 in OBJC_METACLASS_$_NSString ()
#2  0x0000782d in -[ProductListViewController tableView:cellForRowAtIndexPath:] (self=0x7aadf50, _cmd=0x5bf6820, tableView=0x7066e00, indexPath=0x6f52a60) at /Users/leonekeijzer/Documents/Cameraland/Classes/ProductListViewController.m:101
#3  0x00334a3f in -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] ()
#4  0x0032aad2 in -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] ()
#5  0x0033f40c in -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] ()
#6  0x003374bc in -[UITableView layoutSubviews] ()
#7  0x024580d5 in -[CALayer layoutSublayers] ()
nightfox500 is offline   Reply With Quote
Old 08-10-2010, 04:32 PM   #4 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: Switzerland
Age: 26
Posts: 35
Shod is on a distinguished road
Default

It is hard to tell with just looking at the code you posted, you should debug this putting some break point around and inspecting your variables. I can just throw I wild guess:

I see that your doing:

Code:
if (data!=nil) { [data release]; }
and I assume that you are accessing the data instance variable in the NSURLConnection callbacks. Now just doing [data release] won't invalidate the data pointer (the pointer won't be set to nil), so subsequent code that will use the data variable will try to access an invalid memory region, you should change that to:

Code:
if (data!=nil) { [data release]; data = nil;}
but this may not be the problem, as I said before it is kind of hard to debug it without having all the code, and you should set up some break points.
Shod is offline   Reply With Quote
Old 08-10-2010, 04:37 PM   #5 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 34
nightfox500 is on a distinguished road
Default

Quote:
Originally Posted by Shod View Post
It is hard to tell with just looking at the code you posted, you should debug this putting some break point around and inspecting your variables. I can just throw I wild guess:

I see that your doing:

Code:
if (data!=nil) { [data release]; }
and I assume that you are accessing the data instance variable in the NSURLConnection callbacks. Now just doing [data release] won't invalidate the data pointer (the pointer won't be set to nil), so subsequent code that will use the data variable will try to access an invalid memory region, you should change that to:

Code:
if (data!=nil) { [data release]; data = nil;}
but this may not be the problem, as I said before it is kind of hard to debug it without having all the code, and you should set up some break points.
Ok I will give it a try but seen my level of experience I'm not sure how to present or retrieve that information from you. If you can give me the right tips for debugging so I can learn something new and present the right data to you!

Thanks in advance!
nightfox500 is offline   Reply With Quote
Old 08-10-2010, 04:50 PM   #6 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: Switzerland
Age: 26
Posts: 35
Shod is on a distinguished road
Default

there are tutorials that explain how to properly debug a project with xcode, you should find one and go through it.

this could be a start:
Xcode Debugging Guide: Debugging in the Text Editor
Xcode Debugging Guide: Debugging in the Debugger
Shod is offline   Reply With Quote
Old 08-10-2010, 04:55 PM   #7 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 34
nightfox500 is on a distinguished road
Default

Quote:
Originally Posted by Shod View Post
there are tutorials that explain how to properly debug a project with xcode, you should find one and go through it.

this could be a start:
Xcode Debugging Guide: Debugging in the Text Editor
Xcode Debugging Guide: Debugging in the Debugger
Thanks alot, my debugger says that the url argument isn't a CFString. Any suggestions with this info?
nightfox500 is offline   Reply With Quote
Old 08-10-2010, 05:27 PM   #8 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 34
nightfox500 is on a distinguished road
Default

Quote:
Originally Posted by nightfox500 View Post
Thanks alot, my debugger says that the url argument isn't a CFString. Any suggestions with this info?
EDIT:
Here is a screenshot of my debugger when I fix the CFString problem. Only rule 5 contains readable values and I can't read therest. If you can help me with this you can make my day! =D

nightfox500 is offline   Reply With Quote
Old 08-11-2010, 01:18 AM   #9 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: Switzerland
Age: 26
Posts: 35
Shod is on a distinguished road
Default

I can't help you much without having control over the debugger, what I can say to you is to inspect carefully the content of each variable using the window on the right or by hovering with your mouse on the code itself.
Shod is offline   Reply With Quote
Old 08-11-2010, 05:23 AM   #10 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 34
nightfox500 is on a distinguished road
Default

Hey Shod,

I finally found the problem, I didn't provide the right argument type I needed to provide an NSURL. And I tried to connect twice in the loadImageFromURL.
The improved function:

- (void)loadImageFromURLNSURL*)url {
if (![NSThread isMainThread]) {
[self performSelectorOnMainThread:@selector(loadImageFro mURL
withObject:url waitUntilDone:NO];
return;
}

if (connection!=nil) { [connection release]; } //in case we are downloading a 2nd image
if (data!=nil) { [data release]; data = nil;}

NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 30.0];

//This makes it possible to scroll while the images are being loaded
connection = [[NSURLConnection alloc] initWithRequest: request delegate: self startImmediately: NO];
[connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[connection start];
}

Thanks for all the help and tips!
nightfox500 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 350
10 members and 340 guests
bignoggins, Chickenrig, firecall, givensur, iNet, linkmx, michaelhansen, Objective Zero, PlutoPrime, stanny
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,894
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:39 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0