fixed some memory free and error reporting calls.

thanks to fabiano fidencio.