faq icon indicating copy to clipboard operation
faq copied to clipboard

"it seems like jq_start frees input" mystery explanation

Open diafour opened this issue 4 years ago • 1 comments

Hi! Great work! Learn a lot about cgo and jq internals from this project.

I start my own project to use jq programs in Go projects with ability to cache compiled jq state. So here are some findings of jq_start behavior:

jq_start does two important things:

  • it calls jq_reset that free everything on the stack
  • it pushes input jv pointer to the stack

That stack is freed by a jq_teardown call that will call the same jq_reset. But input jv is already freed by the deferred call ;) That is why you need to use jv_copy here: https://github.com/jzelinskie/faq/blob/master/pkg/jq/exec.go#L237

diafour avatar Nov 23 '19 15:11 diafour

That's a great explanation! Thank you so much! Do you want to open a PR that updates the comment? That way the codebase can reflect your contribution of knowledge 😄

jzelinskie avatar Nov 25 '19 20:11 jzelinskie