Your code works fine as you create new stack object of Message each loop iteration. But this place is very dangerous cause user can call this method several times for one object and it will rewrite memory if msg in not NULL. You should check and free memory before reading.