From bdd71691c51f4a69cace090e685e3289e9fe6bbb Mon Sep 17 00:00:00 2001 From: _tid_ Date: Thu, 20 Aug 2015 21:48:36 +0900 Subject: [PATCH] Fixed memory leak #27 fixed #27 --- Source/libxml/libxmlHTMLDocument.swift | 4 ++++ Source/libxml/libxmlHTMLNode.swift | 32 +++++++++++++++++--------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/Source/libxml/libxmlHTMLDocument.swift b/Source/libxml/libxmlHTMLDocument.swift index 9d47d9d..d500f81 100644 --- a/Source/libxml/libxmlHTMLDocument.swift +++ b/Source/libxml/libxmlHTMLDocument.swift @@ -72,6 +72,10 @@ internal final class libxmlHTMLDocument: HTMLDocument { return nil } } + + deinit { + xmlFreeDoc(self.docPtr) + } var title: String? { return at_xpath("//title")?.text } var head: XMLElement? { return at_xpath("//head") } diff --git a/Source/libxml/libxmlHTMLNode.swift b/Source/libxml/libxmlHTMLNode.swift index facea40..6d9a73b 100644 --- a/Source/libxml/libxmlHTMLNode.swift +++ b/Source/libxml/libxmlHTMLNode.swift @@ -29,7 +29,7 @@ libxmlHTMLNode internal final class libxmlHTMLNode: XMLElement { var text: String? { if nodePtr != nil { - return String.fromCString(UnsafePointer(xmlNodeGetContent(nodePtr))) + return libxmlGetNodeContent(nodePtr) } return nil } @@ -44,9 +44,9 @@ internal final class libxmlHTMLNode: XMLElement { var innerHTML: String? { var html: String = "" - html += libxmlGetNodeText(nodePtr.memory.children) + html += libxmlGetNodeText(nodePtr.memory.children) ?? "" html += self.xpath(".//*").first?.toHTML ?? "" - html += libxmlGetNodeText(xmlGetLastChild(nodePtr)) + html += libxmlGetNodeText(xmlGetLastChild(nodePtr)) ?? "" return html } @@ -71,7 +71,7 @@ internal final class libxmlHTMLNode: XMLElement { let mem = attr.memory if let tagName = String.fromCString(UnsafePointer(mem.name)) { if attributeName == tagName { - return String.fromCString(UnsafePointer(xmlNodeGetContent(mem.children))) + return libxmlGetNodeContent(mem.children) } } } @@ -111,17 +111,18 @@ internal final class libxmlHTMLNode: XMLElement { let nodeSet = result.memory.nodesetval if nodeSet == nil || nodeSet.memory.nodeNr == 0 || nodeSet.memory.nodeTab == nil { + xmlXPathFreeObject(result) return XMLNodeSet() } var nodes : [XMLElement] = [] let size = Int(nodeSet.memory.nodeNr) for var i = 0; i < size; ++i { - let node = nodeSet.memory.nodeTab[i] + let node: xmlNodePtr = nodeSet.memory.nodeTab[i] let htmlNode = libxmlHTMLNode(docPtr: docPtr, node: node) nodes.append(htmlNode) } - + xmlXPathFreeObject(result) return XMLNodeSet(nodes: nodes) } @@ -161,11 +162,20 @@ internal final class libxmlHTMLNode: XMLElement { } } -private func libxmlGetNodeText(nodePtr: xmlNodePtr) -> String { +private func libxmlGetNodeText(nodePtr: xmlNodePtr) -> String? { let type = nodePtr.memory.type - if type.value == XML_TEXT_NODE.value, - let text = String.fromCString(UnsafePointer(xmlNodeGetContent(nodePtr))) { - return text + if type.value == XML_TEXT_NODE.value { + return libxmlGetNodeText(nodePtr) } - return "" + return nil +} + +private func libxmlGetNodeContent(nodePtr: xmlNodePtr) -> String? { + let content = xmlNodeGetContent(nodePtr) + if let result = String.fromCString(UnsafePointer(content)) { + content.dealloc(1) + return result + } + content.dealloc(1) + return nil }