Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean encode context before putting to pool #65

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions encode.go
Expand Up @@ -186,6 +186,7 @@ func (e *Encoder) encode(v interface{}) error {
p := uintptr(header.ptr)
ctx.init(p)
err := e.run(ctx, code)
ctx.done()
codeSet.ctx.Put(ctx)
return err
}
Expand Down Expand Up @@ -236,12 +237,10 @@ func (e *Encoder) encode(v interface{}) error {
c = code
}

if err := e.run(ctx, c); err != nil {
codeSet.ctx.Put(ctx)
return err
}
err = e.run(ctx, c)
ctx.done()
codeSet.ctx.Put(ctx)
return nil
return err
}

func (e *Encoder) encodeInt(v int) {
Expand Down
11 changes: 11 additions & 0 deletions encode_context.go
Expand Up @@ -93,6 +93,17 @@ func (c *encodeRuntimeContext) init(p uintptr) {
c.keepRefs = c.keepRefs[:0]
}

// done cleans c.keepRefs, so it can be garbage collected.
//
// It is important to call c.done() once it's not used anymore. Otherwise,
// c.keepRefs maybe kept longer than expected, so if the garbage collector
// run, it will see an invalid pointer.
func (c *encodeRuntimeContext) done() {
for i := range c.keepRefs {
c.keepRefs[i] = nil
}
}

func (c *encodeRuntimeContext) ptr() uintptr {
header := (*sliceHeader)(unsafe.Pointer(&c.ptrs))
return uintptr(header.data)
Expand Down