Skip to content

Commit

Permalink
Fixed memory leak #27
Browse files Browse the repository at this point in the history
fixed #27
  • Loading branch information
tid-kijyun committed Aug 20, 2015
1 parent 30932a1 commit bdd7169
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
4 changes: 4 additions & 0 deletions Source/libxml/libxmlHTMLDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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") }
Expand Down
32 changes: 21 additions & 11 deletions Source/libxml/libxmlHTMLNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}

Expand All @@ -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)
}
}
}
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
}

0 comments on commit bdd7169

Please sign in to comment.