sujith1406
sujith1406

Reputation: 2822

saving a large nsstring file many times crashes the application

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 ;

Upvotes: 0

Views: 171

Answers (2)

Stunner
Stunner

Reputation: 12194

Your problem is this code:

 xml=nil;
 [xml release];

You are setting the variable you want to release to nothing before it gets a chance to be released... Think about it, if you set an object to nil the pointer that used to point to that object no longer points to it. Now you don't have a reference to the object you want to release, which is why a memory leak is resulting in this case. The code should be as follows:

 [xml release];
 xml=nil;

Upvotes: 1

borrrden
borrrden

Reputation: 33421

If you don't see where this is leaking then please switch to ARC. It's as clear as day in the middle:

Alloc xml

Set the variable xml to nil (LEAK!!!!!)

Release the content of xml (which is nil) <--- this does nothing

The 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.

Upvotes: 4

Related Questions