modules/utils: fixed memory leak and possible crash

- patch against 4.0 was provided by Guillaume Bour