lestrrat-go / libxml2

Interface to libxml2, with DOM interface
MIT License
230 stars 55 forks source link

Memory leak in doc.Find()/ctx.Find()? #44

Closed cinupina closed 6 years ago

cinupina commented 6 years ago

Hello!

This is a minimal repro I came up with. Basically, doing a Find() once for a brand new document, repeated many times, would leak memory. I tried with doc.Find() as well as ctx.Find() with the same result.

package main

import (
    "github.com/lestrrat/go-libxml2/parser"
)

func main() {
    data := []byte("<parent><child>The quick brown fox jumps over the lazy dog</child></parent>")
    p := parser.New()

    for i := 0; i < 10000000; i++ {
        _ = read(p, data)
    }
}

func read(p *parser.Parser, data []byte) string {
    doc, _ := p.Parse(data)
    defer doc.Free()

    result, _ := doc.Find("//child/text()")
    value := result.String()
    result.Free()
    return value
}

Memory usage keeps growing without going down. Am I missing a .Free() somewhere? If I don't do Find(), it does not leak. Ideas?

lestrrat commented 6 years ago

@cinupina Sorry for not replying sooner. Could this be caused by #45?

cinupina commented 6 years ago

@lestrrat it could very well be. I'll see if that fixed the problem when I have the chance, and report back. Thanks!

cinupina commented 6 years ago

@lestrrat yes, confirmed that the patch fixed it :+1: Thanks to @gutweiler and you!

lestrrat commented 6 years ago

@cinupina Thanks for confirming!