iPhone - Why do I get a leak? - iphone

I get a leak on the code below, any idea why? I am not allocating anything so why does it leak? It leaks 32 bytes
NSString *username = #"";
NSString *myString = #"some text";
NSRange range = [myString rangeOfString:#"username="];
//instrument shows a leak on the line below
username = [myString substringToIndex:range.location + range.length];
//I never release username or myString

There shouldn't have any leaks, myString and userName is autoreleased already. Maybe it is not released yet but it will be released in some future time

It looks as though the String has been autoreleased - so should get released on the next pass through the event loop (#2 shows an autorelease).

Related

Objective-C memory leak in C methods

Hi all
I get memory leaks on the leaks instrument whenever I trigger this method. Do you think anything is wrong with this??
static NSString* readStringOrDie(InputData *input, size_t length, NSStringEncoding encoding)
{
NSString *str = [[NSString alloc] initWithBytes: readOrDie(input,length)
length: length
encoding: encoding]; // HERE THE LEAK COMES !!
if (!str)
[NSException raise: BERParserException format: #"Unparseable string"];
return [str autorelease];
}
That code is fine. Most likely the actor that receives str retains it and subsequently fails to release it. Instruments knows where memory that has leaked was allocated but doesn't know where it should have been released, so it can provide some slightly unhelpful clues.
Because of using NSException for recoverable error.

instruments showing NSPlaceholderstring leaks

I'm trying to reduce the memory leaks in my app, so i used instruments to find all the leaks. I managed to remove almost all of the leaks, except a very annoying one.
Instruments is telling me that i have a lot of NSPlaceholderstring leaks.
The code that generated the leak (according to instruments) is:
if (nil == storedHash)
{
NSString *description = [[NSString alloc] initWithFormat:#"1 = %# 2= %d", uId, service];
self.storedHash = description; // This line is the leak according to instruments
[description release];
description = nil;
}
return storedHash
storedHash is define like this:
#property(copy) NSString* storedHash;
I tried everything i can think of:
I used retain instead of copy
I used an autorelease allocation of the NSString (stringWithFormat)
I tried wrapping the code with an autorelease pool
Nothing of the above changed the leak. (In some cases the type of the leaks change, but there are still leaks)
Ideas anyone?
Where do you release storedHash? Do you release it in dealloc?
Note that NSPlaceholdeString is an implementation detail; it is the class of the instance returned by NSString's +alloc method.
How about in the dealloc method? Did you release the storedHash in the dealloc method? And how about checking if (nil == self.storedHash)
You should use
#property(nonatomic, retain) NSString* storedHash;
instead copy. #property(copy) didn't release your old NSObject and you should do it yourself:
if (nil == storedHash)
{
NSString *description = [[NSString alloc] initWithFormat:#"1 = %# 2= %d", uId, service];
[self.storedHash release];
self.storedHash = description; // This line is the leak according to instruments
[description release];
// description = nil; // it's unnecessary
}
also you should release storedHash in dealloc.

iphone/ipad memory leak from NSString assigned to itself

I have two leaks shown by the instruments tool. I have looked around on google but I haven't seen exactly my problem on there.
Problem #1:
self.wallText = [[text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
I have tried various configurations of the above line but all leak. I need to do both those trimming operations. 'text' is declared with either #"" or stringWithFormat.
My other issue is with the following line:
NSString * value = [elements objectAtIndex:i+1];
if ([value length] >= 2 && [[value substringToIndex:2] isEqualToString:#"S_"]){
value = [value substringFromIndex:2]; // LEAK HERE
}
I need to get all of the string except for the first 2 characters so I don't know how I could release it first or something... if that is indeed what I should be doing.
I could get away wtih leaks before with previous projects but this one is very memory intensive and I need all the memory I can get!
Any pointers would be greatly appreciated
Did you declare #property (retain) for wallText, did you do [wallText release] in dealloc method?
Double check above things and you will not have leaks any more
For Updated Part:
It is really strange that you have a memory leak there. Because at first, your value points to an autoreleased object then it points to another autoreleased object which I think is fine.
have u use alloc for value. value = [value substringFromIndex:2]; .here now value is referencing to new autorelease string. so u can not release previous object.

query regarding NSString object and memory management

I have the following piece of code:
NSString *tempString = [[NSString alloc] initWithFormat:#"%d/%d/%d",day, month, year];
dateString = tempString;
[tempString release];
NSLog(#"retain count for datstring and tempstring is %d and %d",[dateString retainCount],[tempString retainCount]);
NSLog(#"%# and %#",dateString, tempString)
Now it prints the retain count of tempString as 1 (also dateString retain count = 1) even though I am releasing it in the line before. Also the NSlog statement printing the two strings doesn't display anything. Im assuming this is because, since by my code, dateString is pointing to the location of tempString and tempString has been released, NSlog will be unable to print the string. But then why is the retain count of tempString = 1?
If i replace the line
dateString = tempString;
with
dateString = [NSString stringWithString: tempString];
then the NSlog statement prints the values of both dateString and tempString and displays their retain counts as 1. How is the value of tempString getting printed when i am releasing it in the previous line?
Read the iphone documentation:
You should not use retainCount to
debug memory management issues.
As jamapag states release simply tells the runtime to run dealloc meaning that the memory is marked as reusable not that it has been cleared. For objects with a retainCount of 1, the runtime usually doesnt decrement the retainCount value after the dealloc.
Another thing to note is that you should not send messages to released objects as you are doing with retainCount you are bound to get some unexpected behaviours
When you send release to tempString the objc runtime simply calls dealloc. Just because an object has been sent dealloc doesn't mean its data on the heap is immediately destroyed. It's just marked for destruction later.

NSString potential leak

When I build and analyze my project on XCode, I obtain a 'warning' on the following line:
NSString *contactEmail = (NSString *)ABMultiValueCopyValueAtIndex(emailInfo, 0);
The message is: Potential leak on object allocated on line ... and stored into contactEmail.
Is there any error on that line?
UPDATE
I get the same 'warning' with this line of code:
ABMultiValueRef emailInfo = ABRecordCopyValue(person, kABPersonEmailProperty);
But here, I can't do this:
[emailInfo release];
I'm developing for iPhone.
ABMultiValueCopyValueAtIndex is a "Copy" function, which follows the "Create Rule". You need to call CFRelease to release it after finish using it.
NSString *contactEmail = (NSString *)ABMultiValueCopyValueAtIndex(emailInfo, 0);
...
if (contactEmail != nil)
CFRelease((CFTypeRef) contactEmail);
The cast is somewhat pointless.
The line might leak, unless you release or autorelease it somewhere.
Edit: For brevity:
NSString *contactEmail = [(NSString *)ABMultiValueCopyValueAtIndex(emailInfo, 0) autorelease];
(The cast might still be pointless, I'm unsure as to how the compiler would handle trying sending a message directly to a CFTypeRef.)