I am trying to “tokenize” my data that I get from my text file.
When I am doing this, I get an error like this:
malloc: * error for object 0x844c730: pointer being freed was not allocated
* set a breakpoint in malloc_error_break to debug
The code I use looks like this:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"mydata" ofType:@"txt"];
NSString *rawText = [NSString stringWithContentsOfFile:filePath encoding:NSASCIIStringEncoding error:nil];
//No error was caused by above line
NSArray *tmp = [rawText componentsSeparatedByString:@"##@@"];
NSString *title = @"";
NSString *detail = @"";
for(int i = 0; i < [tmp count]-1; i++)
{
NSArray *base = [[tmp objectAtIndex:i] componentsSeparatedByString:@"##"];
title = [[NSString alloc] initWithFormat:@"%@$$%@",title,[base objectAtIndex:0]];
detail = [[NSString alloc] initWithFormat:@"%@$$%@ | %@ | %@",
title,
[base objectAtIndex:0],
[base objectAtIndex:1],
[base objectAtIndex:2]
];
[base release];
}
[tmp release];
It must be this part of the code, since if I comment this piece out, it works fine.
Reading the error it says set a breakpoint which I have no idea to put that in malloc_error_break
What is wrong in my memory management doing?
Or else how can I split up the string in some other way?
You got
tmpfromcomponentsSeparatedByString:. Since that selector doesn’t start with “alloc” or “new” or “copy” or “mutableCopy”, and since you didn’t do[tmp retain], you don’t owntmp. So you shouldn’t do[tmp release].Same for
base.