Close file to no leak file descriptor memory. Found by Coverity.
Return error if allocation of new element was not possible.

Found by Coverity.