简体   繁体   English

为什么我的代码泄露了?

[英]Why is my code leaking?

UPDATE2 I think I found the true source of the leaks. UPDATE2我想我找到了漏洞的真正来源。 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. 这些字符串属性是从我在这里创建的自定义xlm节点对象复制的(KGYXMLNode)我不明白为什么在这里报告泄漏而不是我的自定义类。 My NSString properties were copy and not retain . 我的NSString属性是copy而不是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. 更新:我认为这是仪器或其他东西的一个错误,或者它不再神奇地泄漏,但是因为xcode 4它没有显示这个泄漏。

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. 我已经围绕某些libxml函数构建了一个objective-c包装器,以便能够使用xpath解析xml文档,并且在这个方法中我正在为我的自定义节点对象设置innerText。


-(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]; 泄漏是NSString *innerText = [[NSString alloc] initWithUTF8String: (char *)content]; . I don't know what is wrong. 我不知道出了什么问题。

You shouldn't access a node's content directly, instead use xmlNodeGetContent : 您不应该直接访问节点的内容,而是使用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;

我不知道为什么你的代码泄漏了,但在我看来,你有一个不安全的自动释放对象分配给obcNode.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. 这是一个猜测,但我认为你的obcnode dealloc方法不会在deallocation上释放它的innerText实例变量。 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. 它会标记innerText的泄漏的原因可能是trimmedText使用与innerText相同的底层unichar数组但具有不同的start和length值,因此它保留innerText以阻止unichar数组消失。

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. 因为trimmedText是一个不可变的字符串,所以向它发送副本只会导致它向自身发送retain并返回它自己,所以obcNode拥有拥有innerText的trimmedText。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM