开发者

Mem leaks even in cases where you dont Alloc or init

Instruments reports that I开发者_运维知识库 get a memory leak in the 2 lines of code at the use substringWithRange. I don't have any alloc,copy or inits explicitly used with this code so I dont understand why this happens.

The memory leaks only appear when they viewController that this code is related to closes.

Here is the offending code:

NSString* path = [[NSBundle mainBundle] pathForResource:@"radio" ofType:@"txt"  
    inDirectory:@""];
NSString* data = [NSString stringWithContentsOfFile:path encoding: 
    NSUTF8StringEncoding error: NULL];      
NSString *nString;

NSString *nHolder;
NSString *iHolder;
NSMutableArray *sHolder = [[[NSMutableArray alloc] init]autorelease];

for (int i=0; i<data.length; i++) 
{
    nString = [data substringWithRange:NSMakeRange(i, 1)];
    if ([nString isEqualToString: comma])
    {
        if (commaCount == 0)
        {
                             // LEAK Reported from the below line
            nHolder = [data substringWithRange:NSMakeRange(i-rangeCount, 
                            rangeCount)];
        }
        else if (commaCount == 1)
        {
                            // LEAK Reported from the below line
            iHolder = [data substringWithRange:NSMakeRange(i-rangeCount, 
                            rangeCount)];
        }
            }
    pInfo  *myInfo = [[[pInfo alloc] init] autorelease];
myInfo = nHolder;
myInfo = iHolder;
  }

And pInfo

@interface pInfo : NSObject 
{
NSString *name;
NSString *info;
}

@property(nonatomic, retain) NSString *name;
@property(nonatomic, retain) NSString *info;

-(id)init;
@end

In the dealloc method of pInfo I dont release anything since I have no allocs.

I would appreciate it if somebody could enlighten me to what I do wrong here.

The stack trace is

-[NSCFString substringWithRange:]

CFStringCreateWithSubstring

__CFStringCreateImmutableFunnel3

_CFRuntimeCreateInstance

Thanks -Code


Instruments is telling you where the leak was allocated, not where it was actually leaked!

You will be looking for a retain that is not balanced by a release. In the Allocations instrument, you can turn on retain count tracking and then look at all the retain/release events and see which retain isn't balanced or is otherwise incorrect.

What do you do with nHolder and iHolder?


I'm not sure where the leak is coming from (too little data).

It looks like you are trying to find the first and second comma in data and then pull out a part of data.

If so just use the string parsing/searching API something like

http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/rangeOfString:options:range:locale:

where the string in the first argument is @"," and the range is 0 to end for the first pass, the second pass the range to search would be where the first comma was found to the end.

After you find the two ranges you are looking for then you can use substringWithRange: to get the interesting string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜