Fix memory leak

Client code is supposed to free the output buffer, so I added some GC callback.. 
But since the output memory buffer can be large, it would make sense to implement the transformations as context managers instead..