returning autorelease NSString still causes memory leaks

Posted by hookjd on Stack Overflow See other posts from Stack Overflow or by hookjd
Published on 2010-03-28T16:00:09Z Indexed on 2010/03/28 16:03 UTC
Read the original article Hit count: 293

I have a simple function that returns an NSString after decoding it. I use it a lot throughout my application, and it appears to create a memory leak (according to "leaks" tool) every time I use it. Leaks tells me the problem is on the line where I alloc the NSString that I am going to return, even though I autorelease it. Here is the function:

-(NSString *) decodeValue
{
 NSString *newString;
 newString = [self stringByReplacingOccurrencesOfString:@"#" withString:@"$"];
 NSData *stateData = [NSData  dataWithBase64EncodedString:newString];
 NSString *convertState = [[[NSString alloc] initWithData:stateData encoding:NSUTF8StringEncoding] autorelease];
 return convertState;
}

My understanding of [autorelease] is that it should be used in exactly this way... where I want to hold onto the object just long enough to return it in my function and then let the object be autoreleased later. So I believe I can use this function through code like this without manually releasing anything:

NSString *myDecodedString = [myString decodeValue];  

But this process is reporting leaks and I don't understand how to change it to avoid the leaks. What am I doing wrong?

© Stack Overflow or respective owner

Related posts about iphone-sdk

Related posts about autorelease