为什么我的代码会泄露?

发布于 2024-10-01 07:53:37 字数 1171 浏览 6 评论 0原文

更新2 我想我找到了泄漏的真正来源。我有一些业务对象具有我忘记释放的字符串属性。这些字符串属性是从我的自定义 xlm 节点对象复制的,在此处创建 (KGYXMLNode) 我不明白为什么在这里报告泄漏而不是我的自定义类。我的 NSString 属性是 copy 而不是 retain

更新:我认为这是仪器或其他东西中的错误,或者它不再神奇地泄漏,但从 xcode 4 开始,它不会显示此泄漏。

您好,根据仪器,我有泄漏下面的代码。我已经围绕某些 libxml 函数构建了一个 Objective-C 包装器,以便能够使用 xpath 解析 xml 文档,并且在这个方法中,我为自定义节点对象设置了 insideText。


-(void) SetInnerTextForNode: (xmlNodePtr) node : (KGYXMLNode *) obcNode
{
  if ((node) && (node->children))
  {
    for (xmlNodePtr pnode = node->children; pnode != NULL; pnode = pnode->next)
    {
      if (pnode->type == XML_TEXT_NODE)
      {
        xmlChar *content = pnode->content;
        NSString *innerText = [[NSString alloc] initWithUTF8String: (char *)content];
        NSString *trimmedText = [innerText stringByTrimmingCharactersInSet: trimCharSet];
        if (trimmedText.length > 0)
          obcNode.innerText = trimmedText;
        [innerText release];
        break;
      }
    }
  }
}

泄漏是 NSString *innerText = [[NSString alloc] initWithUTF8String: (char *)content];。我不知道出了什么问题。

UPDATE2 I think I found the true source of the leaks. I had some business objects that have string properties I forgot to release. These string properties were copied from my custom xlm node object, created here (KGYXMLNode) I don't understandt why the leak is reported here instead of my custom class. My NSString properties were copy and not retain.

UPDATE: I think it was a bug in Instruments or something or it doesn't magically leak anymore, but since xcode 4 it doesn't show this leak.

Hello, according to instruments i have a leak in the following code. I've built an objective-c wrapper around certain libxml functions to be able to parse xml docs using xpath, and in this method I'm setting the innerText for my custom node object.


-(void) SetInnerTextForNode: (xmlNodePtr) node : (KGYXMLNode *) obcNode
{
  if ((node) && (node->children))
  {
    for (xmlNodePtr pnode = node->children; pnode != NULL; pnode = pnode->next)
    {
      if (pnode->type == XML_TEXT_NODE)
      {
        xmlChar *content = pnode->content;
        NSString *innerText = [[NSString alloc] initWithUTF8String: (char *)content];
        NSString *trimmedText = [innerText stringByTrimmingCharactersInSet: trimCharSet];
        if (trimmedText.length > 0)
          obcNode.innerText = trimmedText;
        [innerText release];
        break;
      }
    }
  }
}

The leak is NSString *innerText = [[NSString alloc] initWithUTF8String: (char *)content];. I don't know what is wrong.

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

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

发布评论

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

评论(3

夏九 2024-10-08 07:53:37

您不应直接访问节点的内容,而应使用 xmlNodeGetContent

        xmlChar *content = xmlNodeGetContent(pnode);
        NSString *innerText = [[NSString alloc] initWithUTF8String: (char *)content];
        NSString *trimmedText = [innerText stringByTrimmingCharactersInSet: trimCharSet];
        if (trimmedText.length > 0)
          obcNode.innerText = trimmedText;
        [innerText release];
        // you must free what xmlNodeGetContent returns!
        xmlFree(content);
        break;

You shouldn't access a node's content directly, instead use xmlNodeGetContent:

        xmlChar *content = xmlNodeGetContent(pnode);
        NSString *innerText = [[NSString alloc] initWithUTF8String: (char *)content];
        NSString *trimmedText = [innerText stringByTrimmingCharactersInSet: trimCharSet];
        if (trimmedText.length > 0)
          obcNode.innerText = trimmedText;
        [innerText release];
        // you must free what xmlNodeGetContent returns!
        xmlFree(content);
        break;
安穩 2024-10-08 07:53:37

我不知道为什么你的代码会泄漏,但在我看来,你将自动释​​放的对象不安全地分配给 obcNode.innerText 而不保留它。

I don't know why your code is leaking, but it seems to me that you have an unsafe assignment of an autoreleased object to obcNode.innerText without retaining it.

盛装女皇 2024-10-08 07:53:37

这是一个猜测,但我认为您的 obcnode 的 dealloc 方法在释放时不会释放其 insideText 实例变量。乍一看,您的代码片段对于内存管理来说看起来很好,这是我能看到的唯一潜在错误。

它标记innerText泄漏的原因可能是trimmedText使用与innerText相同的底层unichar数组,但具有不同的起始值和长度值,因此它保留innerText以阻止unichar数组消失。

因为 TrimmedText 是一个不可变的字符串,所以向它发送副本只会导致它向自身发送保留并返回自身,因此 obcNode 拥有 TrimmedText,而 TrimmedText 又拥有innerText。

This is a bit of a guess, but I think your dealloc method for obcnode does not release its innerText instance variable on deallocation. At first glance, your code fragment looks fine for memory management and that is the only potential error I can see.

The reason why it would be flagging the leak for innerText is possibly that trimmedText uses the same underlying unichar array as innerText but with different start and length values and therefore it retains innerText to stop the unichar array from going away.

Because trimmedText is an immutable string, sending copy to it merely causes it to send retain to itself and return itself, so obcNode owns trimmedText which owns innerText.

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