martini-contrib / render

Martini middleware/handler for easily rendering serialized JSON, XML, and HTML template responses.
MIT License
245 stars 57 forks source link

Possible Memory Leak #52

Open SlothNinja opened 9 years ago

SlothNinja commented 9 years ago

Unless I'm missing something, the addYield method leaks memory.

func (r *renderer) addYield(name string, binding interface{}) {
    funcs := template.FuncMap{
        "yield": func() (template.HTML, error) {
            buf, err := r.execute(name, binding)
            // return safe html here since we are rendering our own template
            return template.HTML(buf.String()), err
        },
        "current": func() (string, error) {
            return name, nil
        },
    }
    r.t.Funcs(funcs)
}

r.execute method Gets a buffer from the buffer pool, but the buffer is never Put back to the pool. I suggest getting rid of the r.execute method (it's two lines of code) and essentially in-lining its functionality. Doing so will allow deferring the buffer Put immediately after the the buffer Get. Seeing these paired together makes it much easier to ensure the buffer is returned.