It may leak because ready()  may be called repeatedly if the topn is in a child query.

Should be freeing (and clearing) in the done() method so that the memory is released as soon as no longer required. Realloc sounds like a nice idea but I think you can't without holding on to the memory between child graph executions, which would have the undesirable side effect of holding on to it after the last one for he duration of the rest of the graph.