You are deleting the OstreamOutputStream
while it is still being used by the CodedOutputStream
. You have to delete the objects in the reverse of the order they were created, so that no object is deleted while some other object is still using it.
I suspect that you also have a second problem: you are creating the fstream
as a local variable, which means it will be destroyed as soon as the function returns, making your pointer invalid.