fix some handling after memory allocation

add missing checks after memory allocation
add missing memset after memory allocation