fixed memory leak in read loop