Fix memory leak in compute_weight