Fix a missing unlock in case of error, and a missing free().