Tolkien says Frodo wrote the book, and later Sam finished it. Merry, Pippin and the others told them their tales, and this was how the Red Book was written. If, Tolkien had killed Frodo and Sam, who'd have known what happened in their journey! Also, their deaths wouldn't seem like sacrifice, instead they'd be like victims of the Shadow. This way, Sam was there to tell the story, and Frodo's ultimate sacrifice became a noble one.
P.S. I apologise if someone has posted this thought before, I didn't the entire thread.