NSString内存管理问题

时间:2011-09-18 18:26:49

标签: objective-c memory-management nsstring

我是用C开发的,但对Objective-C和iPhone应用程序开发来说还是一个新手。我正在开发一个需要从字符串中删除标点符号的应用程序。该函数有效,但是当我分析代码时,它会标记我正在使用的一个NS字符串周围的一些问题。

我不明白为什么,因此不知道如何解决它。

主要功能的代码以及分析器警告是:

- (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)];
}

getStartPunct和getEndPunct函数的代码位于

之下
+(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

有人能看出问题所在吗?我确信这是非常基本的东西......

非常感谢!

感谢目前为止的所有回复。

adpalumbo你是对的,我按错了顺序粘贴元素。正确的顺序如下,我已按照Alex Nichol的建议更改了初始化。

这已经修复了1个警告但其他人(如下所示)仍然存在,我不明白为什么'start_punct'和'end_punct'表现不同

- (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 个答案:

没有答案