Two fixes to prevent memory leak from occuring.
Merge commit '1436047f3293f02bde471dc307388e8cb098dec0' into next-master