fixed invalid write issue in d_mat_init