I think you're leaking the return from StringWriterClose(); its implementation remarks <code>
//NOTE: transfer of ownership for allocated return value
</code> which means you have a duty to free() the return at some point, or delegate that duty to someone else; you don't.  Instead, you delegate tidy-up of another copy of the data ...