views:

58

answers:

2

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

+3  A: 

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.

vodkhang
+1 To provide a bit of detail: this works because the `@synthesize`'d setter method with the "retain" semantic releases the current value before setting and retaining the new one. So it's memory-safe to "overwrite" those sorts of properties directly.
Dan Ray
No I wasn't using release in the dealloc method. So is that always the case... Whenever I retain a property on the @property declaration I have to release it in dealloc? Is that the case even for IBoutlet properties?
Mike Simmons
yeah, if you declare a @property(retain) you have to release in dealloc even for IBOutlet. It is not just `@property(retain)` but also if you own some instance variable (by `alloc, retain and copy`) you have to release in dealloc. Also make sure that you didn't overrelease it as well (by always using self.variable for property)
vodkhang
Ok cool good to know thanks for that. I am still getting a leak for my issue # 2. I have updated the code sample.
Mike Simmons
You should create another question that will attract more answers:). People usually not look at the old, updated question
vodkhang
A: 

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.

pawan.mangal
I never use alloc to create a new string. It's always using the literal, assigning from another variable, or using one of the string utility methods.
Mike Simmons