سؤال

I'm sorry if it is a duplicate, I've searched in SO and I've seen that there are similar question but I'm still not able to debug the problem.

I'm using stringstream for simple debugging purpose. I've this macro:

#else 
#include <sstream>
extern std::wstringstream trc;
#define DEBUG_MSG(x) \
    trc.str(std::wstring());\
    trc<<x;\
    OutputDebugString(trc.str().c_str())
#endif 

When I use it like

DEBUG_MSG("IPCFacilities: InsertCtrlMessage: write." <<" Time: "<<GetTickCount64()<<std::endl);

In DebugView I get:

IPCFacilities: InsertCtrlMessage: write. Time: 265793562
IPCFacilities: InsertCtrlMessage: write. Time: 265793562

(the output is printed twice)

What I'm doing wrong?

هل كانت مفيدة؟

المحلول

It's just a race condition issue.

If the code is interleaved like this:

thread 1: trc.str(std::wstring());
thread 2: trc.str(std::wstring());
thread 1: trc<<x;
thread 1: OutputDebugString(trc.str().c_str());
thread 2: trc<<x;
thread 2: OutputDebugString(trc.str().c_str());

the output seems printed twice but it isn't. It's just a bug in my code. As always macro are bad for this kind of things and this time I've learned the lesson. Thanks for participation.

نصائح أخرى

What I can recall I faced similar issue and this is known issue for OutputDebugString. Just change the debugging type to native.

For VS2010

1.In Solution Explorer, select the project.

2.On the View menu, click Properties

3.In the Property Pages dialog box, expand the Configuration Properties node, and then select Debugging.

4.Set Debugger Type to native

Check the below link, the ticket has been closed as not reproducible but there are other who have faced the similar sitiuation

http://connect.microsoft.com/VisualStudio/feedback/details/774425/outputdebugstring-prints-twice-in-the-vs-output-window-on-windows-8

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top