开发者

Issue with NSString memory management

I have developed in C but am quite new to Objective-C and iPhone app development. I am working on an app that needs to strip the punctuation off a string. The function works but when I analyse the code it flags up some issues around one of the NSstrings I am using.

I don't understand why and therefore don't know how to fix it.

The code for the main function along with the analyser warning is:

- (IBAction)doIt {
    NSString *start_punct = [[NSString alloc] init];
    NSString *end_punct = [[NSString alloc] init];
    NSString *actual_word = [[NSString alloc] init];

    outputTextTextView.text = translatedText; //potential leak of an object alloctated on line xx and stored into 'actual word'

    [translatedText release]; translatedText = nil;
    [start_punct release]; start_punct = nil; //incorrect decrement of reference count of an     object that is not owned at this point by the caller
    [end_punct release]; end_punct = nil;
    [actual_word release]; actual_word = nil; //this causes a crash

    start_punct = [MainViewController getStartPunct:word start:&start_range_start len:&start_range_len];
    end_punct = [MainViewController getEndPunct:word start:&end_range_start len:&end_range_len];    
    actual_word = [word substringWithRange: NSMakeRange(start_range_start,(end_range_start-start_range_start)+1)];
}

The code for the getStartPunct and getEndPunct functions is below

+(NSString*) getStartPunct:(NSString*) inputString 
                     start:(NSInteger*)rangeStart 
                       len:(NSInteger*)length {
    NSString* start_str = nil;
    NSRange   firstAlphanumCharFromStart = [inputString rangeOfCharacterFromSet:[NSCharacterSet alphanumericCharacterSet]];

    if (firstAlphanumCharFromStart.location != NSNotFound) {    
        start_str = [inputString substringWithRange: NSMakeRange(0, firstAlphanumCharFromStart.location)];
        *length = firstAlphanumCharFromStart.length;
    *rangeStart = firstAlphanumCharFromStart.location;
    } //if

    if (start_str == nil) {
        *length=0;
        *rangeStart=0;
    }
    return start_str;
} //getStartPunct

+(NSString*) getEndPunct:(NSString*) inputString 
                   start:(NSInteger*)rangeStart 
                     len:(NSInteger*)length {
    NSString* end_str = nil;
    NSInteger rnge = inputString.length;
    NSCharacterSet* CS = [NSCharacterSet  alphanumericCharacterSet];
    NSRange   firstNonAlphanumCharFromEnd = [inputString rangeOfCharacterFromSet:CS  options:NSBackwardsSearch]; 

    if (firstNonAlphanumCharFromEnd.location != NSNotFound) {
        end_str = [inputString substringWithRange:     NSMakeRange(firstNonAlphanumCharFromEnd.location+1, rnge - firstNonAlphanumCharFromEnd.location-1)];
        *length = firstNonAlphanumCharFromEnd.length;
        *rangeStart = firstNonAlphanumCharFromEnd.location;
    } //if

    if (end_str == nil) {
        *length=0;
        *rangeStart=0;
    }
return end_str;
} //getEndPunct

Can someone see what the issue is? I'm sure it is something very basic..

Many Thanks in advance!

Thanks for all the responses so far.

adpalumbo you are right, I had paste the elements in the wrong order. The correct order is below and I have changed the initialization as suggested by Al开发者_高级运维ex Nichol.

This has fixed 1 of the warning but the others (as shown below) still remain and I don't understand why 'start_punct' and 'end_punct' are behaving differently

- (IBAction)doIt {  
NSString *start_punct = nil;
NSString *end_punct = nil;
NSString *actual_word = nil;

start_punct = [MainViewController getStartPunct:word start:&start_range_start len:&start_range_len]; // method returns objective with +0 retain count
end_punct = [MainViewController getEndPunct:word start:&end_range_start len:&end_range_len];
actual_word = [word substringWithRange: NSMakeRange(start_range_start,(end_range_start-start_range_start)+1)];

[translatedText release]; translatedText = nil;
[start_punct release]; start_punct = nil; //incorrect decrement of reference count
[end_punct release]; end_punct = nil;
//[actual_word release]; actual_word = nil; //possible abend
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜