From 21b8c7719e29de2380dea7fa4bd49f8eded84b51 Mon Sep 17 00:00:00 2001 From: Florent Rougon Date: Sun, 14 Jan 2018 15:06:45 +0100 Subject: [PATCH] FGMouseInput: fix two uses of std::ostringstream Seeking to the beginning doesn't clear the contents. Note that we don't use std::ostringstream::str() either, because apparently std::ostringstream is too complex to be easily reset to a clean state, so we just recreate an instance every time we need. cf. https://stackoverflow.com/a/12112642/4756009 and https://stackoverflow.com/questions/624260/how-to-reuse-an-ostringstream/624291#comment437178_624276 --- src/Input/FGMouseInput.cxx | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Input/FGMouseInput.cxx b/src/Input/FGMouseInput.cxx index 68ab15b78..879fe3dfd 100644 --- a/src/Input/FGMouseInput.cxx +++ b/src/Input/FGMouseInput.cxx @@ -390,7 +390,10 @@ void FGMouseInput::init() m.mode_node->setIntValue(0); } for (j = 0; j < MAX_MOUSE_BUTTONS; j++) { - buf.seekp(ios_base::beg); + // According to and other + // similar questions on stackoverflow.com, it seems safer not to try to + // reuse the 'buf' variable we have above. + std::ostringstream buf; buf << "/devices/status/mice/mouse["<< i << "]/button[" << j << "]"; m.mouse_button_nodes[j] = fgGetNode(buf.str().c_str(), true); m.mouse_button_nodes[j]->setBoolValue(false); @@ -413,9 +416,8 @@ void FGMouseInput::init() // Read the button bindings for this mode m.modes[j].buttons.reset(new FGButton[MAX_MOUSE_BUTTONS]); - std::ostringstream buf; for (k = 0; k < MAX_MOUSE_BUTTONS; k++) { - buf.seekp(ios_base::beg); + std::ostringstream buf; buf << "mouse button " << k; m.modes[j].buttons[k].init( mode_node->getChild("button", k), buf.str(), module ); }