If you can't allocate on the stack, the easiest way to avoid leaking memory is to wrap the data structure in a ruby object, even if it is only used temporarily.
A simple example of this is in the strftime function.