Fix leaking resource "pid"

pid after being allocated, missed to be freed at many places where return is called.
Applied fix.