OutputDebugString导致不一致的错误

问题描述:

我有一个问题,出于神秘的原因出现和消失。在我开始项目的时候,我发现了一个相当方便的函数,它允许在VS2010中调试窗口输出。它工作了一段时间。OutputDebugString导致不一致的错误

现在它显示错误不一致。这意味着有时代码会被编译,有时代码不会,并且我无法找出为什么下面的代码导致错误。看起来几乎是随机的。按编译,错误,再次按编译而不改变任何东西有时错误有时罚款。

这是它看起来像错误:

http://clip2net.com/clip/m0/1332710747-clip-29kb.png

,无:

http://clip2net.com/clip/m0/1332737362-clip-40kb.png

罪魁祸首就是OutputDebugString的(BUF); 该行注释掉时不会发生错误。

我正在寻求解决这个问题,我只是需要一种方法来输出文本到调试窗口(输出),并寻找一个简单,稳定的解决方案。或者也许有一种方法可以使这个功能起作用。我有点卡住了。

如果你能分享你如何做,我将不胜感激。

的代码是:

#pragma once 
#ifndef _XDEBUG_H_ 
#define _XDEBUG_H_ 

#include <stdio.h> 
#include <stdarg.h> 
#include <ctype.h> 


class XDebug 
{ 
public: 

    static void __cdecl odprintf(const wchar_t *format, ...){ 
    wchar_t buf[4096], *p = buf; 
    va_list args; 
    int  n; 

      va_start(args, format); 
      n = _vsnwprintf(p, sizeof buf - 3, format, args); // buf-3 is room for CR/LF/NUL 
      va_end(args); 

      p += (n < 0) ? sizeof buf - 3 : n; 

      while (p > buf && isspace(p[-1])) 
        *--p = '\0'; 

      *p++ = '\r'; 
      *p++ = '\n'; 
      *p = '\0'; 

      OutputDebugString(buf); 
    } 

}; 

#endif 

它看起来像你没有这样做:

#include <windows.h> 

OutputDebugString功能是Windows API的一部分。

OutputDebugStringWINDOWS.H定义。您需要包含该标题才能使用该功能。

+0

解决了它,谢谢。仍然不明白为什么它会偶尔编译。 欣赏它! – 2012-03-26 05:01:26