TBXML iOS Parser中的内存泄漏

时间:2011-06-10 13:47:47

标签: iphone objective-c ios memory-leaks instruments

我正在使用TBXML进行XML Feed解析和检测内存泄漏的工具。

仪器告诉我功能中存在内存泄漏

- (TBXMLAttribute*) nextAvailableAttribute {
currentAttribute++;

if (!currentAttributeBuffer) {
currentAttributeBuffer = calloc(1, sizeof(TBXMLAttributeBuffer));
currentAttributeBuffer->attributes = (TBXMLAttribute*)calloc(MAX_ATTRIBUTES,sizeof(TBXMLAttribute));
currentAttribute = 0;
} else if (currentAttribute >= MAX_ATTRIBUTES) {
currentAttributeBuffer->next = calloc(1, sizeof(TBXMLAttributeBuffer));
currentAttributeBuffer->next->previous = currentAttributeBuffer;
currentAttributeBuffer = currentAttributeBuffer->next;
currentAttributeBuffer->attributes = (TBXMLAttribute*)calloc(MAX_ATTRIBUTES,sizeof(TBXMLAttribute));
currentAttribute = 0;
}

return &currentAttributeBuffer->attributes[currentAttribute];
}

currentAttributeBuffer->attributes = (TBXMLAttribute*)calloc(MAX_ATTRIBUTES,sizeof(TBXMLAttribute));

之前有人解决了吗?

2 个答案:

答案 0 :(得分:1)

警告....仪器告诉你最初创建泄漏内存的地方,而不是问题在那一行...例如,如果你对返回的属性做了什么并将其泄漏到其他地方,仪器将显示上面的线,它创建的地方,不是你泄露它的地方....

答案 1 :(得分:0)

calloc是一个分配内存的C函数。完成后,您应该致电free currentAttributeBuffer;。这将解决内存泄漏问题。