the following code crashes.Basically i am accessing a very large string(xml -contains image data) saved as a file…modifying it and saving it in a new name… when profiling i didnot see any leak with this code… but after repeating this process for 20-25 times… the app crashes on iphone 3gs with memory warning level three and it kills the whole apps running too… i cant find any place where this app is leaking to cause a memory warning ….any suggestions
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documents = [paths objectAtIndex:0];
NSString *filename = [NSString stringWithFormat:@"%@.yyy",fileToDuplicate];
NSString *initPath = [documents stringByAppendingPathComponent:filename];
NSString *final = [NSString stringWithFormat:@"%@.yyy",[[alertView textFieldAtIndex:0] text]];
NSString *finalPath = [documents stringByAppendingPathComponent:final];
NSString *newName=[[[alertView textFieldAtIndex:0] text] copy];
NSError *error;
NSString *xml = [[NSString alloc] initWithContentsOfFile:initPath encoding:NSASCIIStringEncoding error:&error] ;
NSString *xml_1=[xml stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"<file_name><name>%@.yyy</name></file_name>",fileToDuplicate] withString:[NSString stringWithFormat:@"<file_name><name>%@.yyy</name></file_name>",newName]];
NSString *xml_2=[xml_1 stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"<property_name>%@</property_name>",fileToDuplicate] withString:[NSString stringWithFormat:@"<property_name>%@</property_name>",newName]];
[xml_2 writeToFile:finalPath atomically:YES encoding:NSASCIIStringEncoding error:&error];
xml=nil;
[xml release];
xml_1=nil;
xml_2=nil;
[self.mTableView reloadData];
fileToDuplicate=@"";
[newName release];
[pool drain];
return ;
If you don’t see where this is leaking then please switch to ARC. It’s as clear as day in the middle:
Alloc
xmlSet the variable
xmlto nil (LEAK!!!!!)Release the content of
xml(which is nil) <— this does nothingThe last two are reversed. You need to release it before you set it to nil. I suggest reading a little more about pointers if you do not understand this concept. The release message acts on the content of the pointer, not the pointer itself. The latter wouldn’t make sense.