Stash away the output of the render-to-texture operation.  Also unbind the texture object from the framebuffer before unbinding the framebuffer object from gl.FRAMEBUFFER (in cleanup code), in case that matters.