fix invalid memory allocation