Fix reference leaks introduced by the patch for issue #5308.