This is essentially a backport of <f52b6c944e> by Eelco Dolstra <eelco.dolstra@logicblox.com>. The use-after-free bug would typically manifest when building with GCC 5.1.
f52b6c944e