NSScanner 类别方法中的泄漏

发布于 2024-08-25 10:51:35 字数 950 浏览 2 评论 0原文

我创建了一个 NSScanner 类别方法来显示仪器中的泄漏。


- (BOOL)scanBetweenPrefix:(NSString *)prefix 
                andSuffix:(NSString *)suffix 
               intoString:(NSString **)value
{
    NSCharacterSet *charactersToBeSkipped = [self charactersToBeSkipped];
    [self setCharactersToBeSkipped:nil];

    BOOL result = NO;

    // find the prefix; the scanString method below fails if you don't do this
    if (![self scanUpToString:prefix intoString:nil])
    {
        MY_LOG(@"Prefix %@ is missing.", prefix);
        return result;
    }

    //scan the prefix and discard
    [self scanString:prefix intoString:nil];

    // scan the important part and save it
    if ([self scanUpToString:suffix intoString:value]) // this line leaks
    {
        result = YES;
    }
    [self setCharactersToBeSkipped:charactersToBeSkipped];
    return result;
}

我认为这是我将值传递给方法或从方法传递值的方式,但我不确定。这是一个小泄漏(32 字节),但如果可以的话我想正确地做到这一点。提前致谢。

I created an NSScanner category method that shows a leak in instruments.


- (BOOL)scanBetweenPrefix:(NSString *)prefix 
                andSuffix:(NSString *)suffix 
               intoString:(NSString **)value
{
    NSCharacterSet *charactersToBeSkipped = [self charactersToBeSkipped];
    [self setCharactersToBeSkipped:nil];

    BOOL result = NO;

    // find the prefix; the scanString method below fails if you don't do this
    if (![self scanUpToString:prefix intoString:nil])
    {
        MY_LOG(@"Prefix %@ is missing.", prefix);
        return result;
    }

    //scan the prefix and discard
    [self scanString:prefix intoString:nil];

    // scan the important part and save it
    if ([self scanUpToString:suffix intoString:value]) // this line leaks
    {
        result = YES;
    }
    [self setCharactersToBeSkipped:charactersToBeSkipped];
    return result;
}

I figure it's the way I'm passing the value to/from the method, but I'm not sure. It's a small leak (32 bytes), but I'd like to do this right if I can. Thanks in advance.

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

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

发布评论

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

评论(2

筱武穆 2024-09-01 10:51:35

我找到了答案。我有一个模型类,它使用上面代码的结果,但忘记在 dealloc 方法中释放该属性。我应该用 Instruments 捕捉到它,但我不知道该去哪里寻找。我陷入了扩展详细信息中的调用堆栈中,其中只有我需要的部分信息。

对于像我这样的白痴,这就是我所做的:

  1. 使用 Instruments 运行您的应用程序......泄漏。
  2. 在仪器中,观察泄漏的块视图(底部的网格图标)并打开扩展细节。
  3. 如果您有多个泄漏对象,请单击显示三角形,以便您可以查看离散地址。
  4. 地址旁边会有一个详细箭头。单击它。
  5. 现在你将回顾历史。它从 Malloc 开始,向您展示每个保留和释放。

您应该有一个 malloc 的释放版本和历史记录中的每个保留的释放版本。匹配你的保留和释放并寻找奇怪的东西。如有疑问,请查看页面底部并仔细检查您可能忘记在 dealloc 方法中释放的任何属性。

I found the answer. I had a model class that used the result of the code above, but forgot to release the property in the dealloc method. I should have caught it with Instruments, but I didn't know where to look. I got caught up in the call stack in the Extended Details, which had only part of the info I needed.

For morons like me, here's what I did:

  1. Run your app with Instruments ... Leaks.
  2. In Instruments, watch the leaked blocks view (the grid icon at the bottom) and turn on the extended detail.
  3. If you have multiple leaked objects, click the disclosure triangle so you can look at a discrete address.
  4. Next to an address, there will be a detail arrow. Click on it.
  5. Now you'll be looking at history. It starts with Malloc and shows you each retain and release.

You should have a release for the malloc and a release for every retain in the history. Match up your retains and releases and look for the oddball. When in doubt, look toward the bottom of the page and carefully review any properties you may have forgotten to release in your dealloc method(s).

北笙凉宸 2024-09-01 10:51:35

好吧,我傻了。此方法不会泄漏。泄漏位于我的调用堆栈的更深处,但我不知道在哪里。抱歉过早发帖。如果我自己无法解决问题,当我弄清楚真正的泄漏在哪里时,我会跟进。

为了回答 Yuji 的评论,我的方法调用是:


    NSString *title;
    [fileScanner scanBetweenPrefix:kTitlePrefix 
                         andSuffix:kTitleSuffix 
                        intoString:&title];

是 &title 的问题吗?我根据现有的 NSScanner 方法和调用对其进行了模式化。

OK, I'm stupid. This method does not leak. The leak is farther down my call stack, but I can't figure out where. Sorry for the premature post. I'll follow up when I figure out where the real leak is, if I can't figure out the fix myself.

In answer to Yuji's comment, my method call is:


    NSString *title;
    [fileScanner scanBetweenPrefix:kTitlePrefix 
                         andSuffix:kTitleSuffix 
                        intoString:&title];

Is it the &title that's the problem? I patterned it after the existing NSScanner methods and calls.

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