I have a very very strange problem and I am without a clue as to why its happening. Simply enough I have a class which stores an NSString and an NSURL. There can be many of these classes so I store them in a mutable array.
This array is used to populate a tableview. When a user hits a table cell I get one of the list class's from the array, get its URL property, and pass that to a detail view. This works fine 99% of the time but the other 1% of the time the URL property that I'm passing sometimes has become released.
Specifically in the debugger the NSURL says "invalid summary" and its urlstring property says ""
I've commented out every place that I release the NSURL. Oddly enough the NSString variable in the same class does not suffer from the same unwanted behavior.
// list data
@interface ListData : NSObject {
NSURL *URL;
NSString *Title;
}
@property(nonatomic,retain) NSURL *URL;
@property(nonatomic,copy) NSString *Title;
@end
#import "ListData.h"
@implementation ListData
@synthesize URL;
@synthesize Title;
-(void)dealloc
{
[URL release];
[Title release];
[super dealloc];
}
@end
// table delegate method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ListData *rld=nil;
rld=[listDataArray objectAtIndex:indexPath.row];
if (self.listView) {
//[remixView closeRemix];
[self.listView release];
self.listView=nil;
}
// occassionaly the value of rld.URL is being released somehow
self.listView = [[ListView alloc] showWithData:rld.URL ];
self.listView.title = rld.remixTitle;
[table.navigationController pushViewController:listView animated:YES];
}
//population of list array
listArray =[[NSMutableArray alloc]init];
while (listelement!=nil) {
listData *rld = [listData alloc];
[rld setURL:[NSURL URLWithString:[TBXML textForElement:[TBXML childElementNamed:@"url" parentElement:listelement]]]];
[rld setTitle:[TBXML textForElement:[TBXML childElementNamed:@"title" parentElement:listelement]]];
[listArray addObject:rld];
[rld release];
listelement = [TBXML nextSiblingNamed:@"list" searchFromElement:listelement];
}
EDIT 1
I think I found the problem but I dont really understand whats going on.
In the listview showWithData method I was passing in a url and assigning it to a nsurl var of listview like this.
-(id) showWithData:(ListData *) data;
{
if ( [super init]) {
baseURL=data.URL;
}
baseURL was being released somehow (in the bowels of TBXML I think) and this was making the NSURL from the parent tableview class be released.
I changed the showWithData method to the following and the problem has ceased to occur. It seems that using 'copy' protected the original NSURL from being released. I dont understand it though because when I check in the debugger both the NSURl being passed to the method and the baseURL var both have the same memory address. Can somebody explain this ?
-(id) showWithData:(xListData *) data;
{
if ( [super init]) {
self.baseURL=[data.URL copy];
}