fix invalid free on certain conditions

Signed-off-by: Luka Perkov <luka.perkov@sartura.hr>