Be more careful about freeing resources earlier