发现 UITextView 委托方法中存在泄漏。对解决方案感到困惑

发布于 2024-11-27 11:54:44 字数 1556 浏览 0 评论 0原文

我在基于导航的应用程序中遇到了 UITextView 及其委托方法之一的问题:

- (BOOL)textView:aView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

我设法限制用户可以使用上述方法输入的文本的最大长度。但我认为我正在使用泄漏数组。

问题是: 我想在用户输入文本视图的最后一行时立​​即保存键入的字符数。然后,我使用该值来计算字符串长度 - 我将其与文本视图的内容大小进行比较以设置限制。该代码工作正常 - 但由于它内部的方法会随着每个文本输入而更新,因此我无法在正确的时刻释放数组。

这是一些代码:

if (numLines == 9)
{
    if (!numCharsArray) 
    {
        numCharsArray = [[NSMutableArray alloc] initWithCapacity:1]; // Stack trace gives this line 3,3% of the leak.
    }

    numChars = tView.text.length;
    NSNumber *number = [[NSNumber alloc] initWithInteger:numChars]; // This line gets 77,3%.
    [numCharsArray addObject:number]; // This line gets the rest, 24,3%.
    [number release];

    startChars = [[numCharsArray objectAtIndex:0] integerValue];

    NSString *lastLine = [[NSString alloc]initWithString:[[tView text] substringFromIndex:startChars]];
    CGSize lineSize = [lastLine sizeWithFont:tView.font forWidth:tView.contentSize.width lineBreakMode:UILineBreakModeWordWrap];
    [lastLine release];

    if (range.length > text.length) 
    {
        return YES;
    }
    else if (numLines == 9 && lineSize.width >= tView.contentSize.width - 45)
    {
        return NO;
    }
}
else
{
    numCharsArray = nil;
    /* 
    if(numCharsArray)
    {
        [numCharsArray release];
    }
    */
}

我尝试了上面注释掉的语句,但是一旦我离开文本视图的最后一行,就会导致应用程序崩溃。正如您在代码注释中看到的那样 - 在不释放数组的情况下,我得到了泄漏。

那么,如何以及在何处正确释放该数组 - 当用户在最后一行时保持其安全?

I've got a problem with an UITextView and one of its delegate methods in my navigation based app:

- (BOOL)textView:aView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

I managed to limit the max length of text the user can input using the above method. But I'm using a leaky array for that matter I think.

The problem is:
I want to save the amount of typed characters right in the very moment the user enters the last line of my textview. I then use that value to calculate the string length - which I compare to the textview's content size to set a limit. The code works fine - but since the method it's inside of is updating with every text input, I'm having trouble releasing the array in the right moment.

Here's some code:

if (numLines == 9)
{
    if (!numCharsArray) 
    {
        numCharsArray = [[NSMutableArray alloc] initWithCapacity:1]; // Stack trace gives this line 3,3% of the leak.
    }

    numChars = tView.text.length;
    NSNumber *number = [[NSNumber alloc] initWithInteger:numChars]; // This line gets 77,3%.
    [numCharsArray addObject:number]; // This line gets the rest, 24,3%.
    [number release];

    startChars = [[numCharsArray objectAtIndex:0] integerValue];

    NSString *lastLine = [[NSString alloc]initWithString:[[tView text] substringFromIndex:startChars]];
    CGSize lineSize = [lastLine sizeWithFont:tView.font forWidth:tView.contentSize.width lineBreakMode:UILineBreakModeWordWrap];
    [lastLine release];

    if (range.length > text.length) 
    {
        return YES;
    }
    else if (numLines == 9 && lineSize.width >= tView.contentSize.width - 45)
    {
        return NO;
    }
}
else
{
    numCharsArray = nil;
    /* 
    if(numCharsArray)
    {
        [numCharsArray release];
    }
    */
}

I tried the out-commented statement above, but that gives me an app crash once I leave the last line of the textview. And as you can see in the code comments - without releasing the array I get a leak.

So how and where do I release that array correctly - keeping it safe while the user is on the last line?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北座城市 2024-12-04 11:54:44

只需替换

第一个

numCharsArray = [NSMutableArray array]; // you do not need to release 
                                           //explicitly as its autorelease numberWithInt

第二个即可

NSNumber *number = [NSNumber numberWithInt:numChars]; //autorelease

NSString *lastLine = [[tView text] substringFromIndex:startChars];

Just replace with

first one

numCharsArray = [NSMutableArray array]; // you do not need to release 
                                           //explicitly as its autorelease numberWithInt

second one

NSNumber *number = [NSNumber numberWithInt:numChars]; //autorelease

NSString *lastLine = [[tView text] substringFromIndex:startChars];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文