NSString , EXC_BAD_ACCESS and stringByAppendingString
i wrote the following code :
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSDictionary *reportDic = [jsonparser objectWithString:json_string error:nil];
NSDictionary *reporttable = [reportDic objectForKey:@"reporttable"];
NSArray *rows = [reporttable objectForKey:@"rows"];
NSString *table = [[NSString alloc]initWithString:@"<table>"] ;
for (NSDictionary *row in rows)
{
table = [table stringByAppendingString:@"<tr>"];
NSArray *cells = [row objectForKey:@"cells"];
for (NSString *cell in cells){
table = [table stringByAppendingString:@"<td>"];
table = [table stringByAppendingString:cell];
table = [table stringByAppendingString:@"</td>"];
}
table = [table stringByAppendingString:@"</tr>"];
index++;
}
table = [table stringByAppendingString:@"</table>"];
return [table autorelease];
the rows are json response parsed by jsonlib this string is returned to a viewcontroller that loads it into a webview ,but i keep getting exc_bad_access for this code on completion of the method that calls it , i don't even have to load it to the webview just calling this method and not using the nsstring causes the error on completion of the calling method... any help will be apperciated thanks .
- (void)viewDidLoad {
[super 开发者_如何转开发viewDidLoad];
AMAppDelegate *appDelegate = (AMAppDelegate*)[[UIApplication sharedApplication] delegate];
NSString * data = [appDelegate fetchTable:name psw:psw];
[webView loadHTMLString:@"this is html" baseURL:nil];
[data release];
}
with or without the release , it doesn't matter
Your memory management is wrong.
Change:
NSString *table = [[NSString alloc]initWithString:@"<table>"] ;
to:
NSString *table = [[[NSString alloc]initWithString:@"<table>"] autorelease];
Then change:
return [table autorelease];
to:
return table;
The reason is the table you're autoreleasing at the end is the one constructed by the previous line:
table = [table stringByAppendingString:@"</table>"];
You're therefore autoreleasing this twice (stringByAppendingString:
returns an autoreleased instance), and not releasing the original table at all.
How about constructing the table using an NSMutableString? One problem with your current approach is that you'll flood the autorelease pool unnecessarily.
NSMutableString *table = [NSMutableString string];
[table appendString:@"<table>"];
for (NSDictionary *row in rows)
{
[table appendString:@"<tr>"];
NSArray *cells = [row objectForKey:@"cells"];
for (NSString *cell in cells){
[table appendFormat:@"<td>%@</td>", cell];
[table appendString:@"</tr>"];
index++;
}
[table appendString:@"</table>"];
return table;
Regarding the update, if you correctly incorporated tewhas suggestions, -fetchTable:psw:
returns an autoreleased NSString
, thus you shouldn't release it in the calling method:
NSString *data = [appDelegate fetchTable:name psw:psw]
// ...
[data release]; // don't do that, data is autoreleased
-fetchTable:psw:
should handle table
like this:
NSString *table = [NSString stringWithString:@"<table>"] ;
// ... leave the rest as it is
return table;
Note that you are also leaking json_string
, you should -release
or -autorelease
it.
精彩评论