I had this issue with SD cards going read-only or just parts of them going read- only a few times back in the early Pi days. Nothing would fix them and nobody offered a suggestion as to why this happened. Apart from the mantra of "Use a good power supply, use good SD cards, always shutdown cleanly"
In recent times it has not happened to my collection of Pi 3. I'm pretty careless about yanking the power at any time. Deliberately, I'm curious to see how well they stand up.
For important things I think making the SD read only is a very good idea, almost essential. I recently made a read only SD file system using
ejolson's instructions here: viewtopic.php?t=161416
. Works well.
If I needed storage on a read-only root Pi I would add another storage device, a USB stick or SSD, for that data.
Other than that, follow the mantra, "Use a good power supply, use good SD cards, always shutdown cleanly"
Memory in C++ is a leaky abstraction .