func (document *XmlDocument) Free() { //must free the xpath context before freeing the fragments or unlinked nodes //otherwise, it causes memory leaks and crashes when dealing with very large documents (a few MB) if document.XPathCtx != nil { document.XPathCtx.Free() document.XPathCtx = nil } //must clear the fragments first //because the nodes are put in the unlinked list if document.fragments != nil { for _, fragment := range document.fragments { fragment.Remove() } } document.fragments = nil var p *C.xmlNode if document.UnlinkedNodes != nil { for p, _ = range document.UnlinkedNodes { C.xmlFreeNode(p) } } document.UnlinkedNodes = nil if document.Ptr != nil { C.xmlFreeDoc(document.Ptr) document.Ptr = nil } }
func (document *XmlDocument) Free() { //must clear the fragments first //because the nodes are put in the unlinked list if document.fragments != nil { for _, fragment := range document.fragments { fragment.Remove() } } document.fragments = nil var p *C.xmlNode if document.UnlinkedNodes != nil { for p, _ = range document.UnlinkedNodes { C.xmlFreeNode(p) } } document.UnlinkedNodes = nil if document.XPathCtx != nil { document.XPathCtx.Free() document.XPathCtx = nil } if document.Ptr != nil { C.xmlFreeDoc(document.Ptr) document.Ptr = nil } }
func (x *XPathContext) evalXPath(expr *XPathExpression) (*XPathObject, error) { if expr == nil { return nil, errors.New("empty XPathExpression") } // If there is no document associated with this context, // then xmlXPathCompiledEval() just fails to match ctx := x.ptr if ctx.node != nil && ctx.node.doc != nil { ctx.doc = ctx.node.doc } if ctx.doc == nil { ctx.doc = C.xmlNewDoc(stringToXmlChar("1.0")) defer C.xmlFreeDoc(ctx.doc) } res := C.xmlXPathCompiledEval(expr.ptr, ctx) if res == nil { return nil, errors.New("empty result") } return &XPathObject{ptr: res}, nil }
func ParseHTMLString(content string) (*Document, error) { d := htmlReadDoc(content, "", "", DefaultHtmlParseFlags) root, err := C.xmlDocGetRootElement(d) if err != nil || root == nil { C.xmlFreeDoc(d) return nil, err } return &Document{ptr: d, root: root}, nil }
func (d *Document) Free() { C.xmlFreeDoc(d.ptr) d.ptr = nil d.root = nil }
func closeDoc(doc *C.xmlDoc) { C.xmlFreeDoc(doc) }
func XmlFreeDoc(d *C.xmlDoc) { C.xmlFreeDoc(d) }
// xmlFreeDoc func (doc *Document) Free() { C.xmlFreeDoc(doc.Ptr) doc.Ptr = nil doc.Node = nil }
// Free the libxml2 allocated objects func (xp *HtmlXp) free() { C.xmlXPathFreeContext(xp.xpathCtx) xp.xpathCtx = nil C.xmlFreeDoc(xp.doc) xp.doc = nil }