在我的ipad中释放内存

时间:2011-09-13 13:17:45

标签: ipad

在ipad的所有朋友即时通讯中,请告诉我为什么我不能发布json var?

NSData* myMutableData = [NSURLConnection sendSynchronousRequest:request returningResponse:&WSresponse error:&WSerror];
NSLog(@"slt%d",[myMutableData length]);
json_string=nil;
json_var = [[NSString alloc] initWithData:myMutableData encoding:NSUTF8StringEncoding ];

json_var = [json_var stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
json_var = [json_var stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
json_var = [json_var stringByReplacingOccurrencesOfString:@"<?xml.......  string\">"  withString:@""];
json_var = [json_var stringByReplacingOccurrencesOfString:@"</return></ns1:getDatasResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>" withString:@""];
    json_var = [json_var stringByReplacingOccurrencesOfString:@"<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>" withString:@""];

json_var = [json_var stringByReplacingOccurrencesOfString:@"<?xml version=&quot;1.0&quot; encoding=&quot;windows-1252&quot;?>" withString:@"<?xml version=\"1.0\" encoding=\"windows-1252\"?>"];



    NSString *data13=[[NSString alloc] initWithString:json_var]; 
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
                    NSString *documentsDirectory = [paths objectAtIndex:0];
                    IDComp = [IDComp stringByReplacingOccurrencesOfString:@"/" withString:@":"];        
                    NSString *test1 = [IDComp stringByAppendingString:@".xml"];
                    NSString *test2 = [documentsDirectory stringByAppendingString:@"/"];
                    NSString *test3 = [test2 stringByAppendingString:test1];
                    NSString *file2 = [NSString stringWithFormat:test3];
                    [data13 writeToFile:file2 atomically:YES];
                    [data13 release];
                    [json_var release];

1 个答案:

答案 0 :(得分:0)

您不需要释放字符串json_var,因为您为其分配了自动释放的对象(方法stringByReplacingOccurrencesOfString:会返回自动释放的NSString)。

如果你不想要下一次应该做的内存泄漏:

  1. 删除行[json_var release]
  2. 替换

    json_var = [[NSString alloc] initWithData:myMutableData encoding:NSUTF8StringEncoding ];
    

    json_var = [[[NSString alloc] initWithData:myMutableData encoding:NSUTF8StringEncoding ]autorelease];