0

因此,在将这个应用程序放在一起的某个地方,当调用 cstring 类成员的析构函数时,我开始遇到运行时检查失败堆栈损坏。我已经到了试图通过在问题上扔砖头来调试它的地步,但仍然没有根源导致它。目前,cstring 所在的类除了初始化其私有字符串成员并将指向另一个类的指针设置为 NULL 之外什么都不做。

有趣的是,如果我不将类指针设置为 NULL 并注释掉该行,损坏就会消失。我认为这有点像红鲱鱼,当编译器拉入包含 CLog 定义的 .h 文件时,编译器将代码放在一起的方式正在发生变化,因为我声明了一个指向那个物体。

    int _tmain(int argc, _TCHAR* argv[])
{

    DWORD a = 0xBABA; //just to help catch the corrupter
    DWORD b = 0xDFDF;

    CStringW startat = L"\\\\anetworkshare\\fre";
    CStringW lookfor = L".inf";

    DirEnum myEnum(startat,lookfor);

    ULONG en = a + b;

    en = a - b;

    return 0;
}

DirEnum.cpp


DirEnum::DirEnum(CString startingdir,CString fileFilter)
{
    m_plogfile = NULL;  //If you comment out this line corruption goes away
    m_startingdir = L"";
    m_extfilter = L"";

    if(startingdir.GetLength() > 0)
        m_startingdir = startingdir;

    if(fileFilter.GetLength() > 0)
        m_extfilter = fileFilter;

    //following commented out to tshoot
    //CLogBase& ref = ref.GetInstance();
    //logBase = &ref;

    //m_plogfile = new CLog(L"DirEnumerator",L"logfile.txt",logINFO);

}

现在我怀疑 log.h 文件中的某些内容导致 ATL 或 CString 库中发生更改,但我不知道是什么。这是 log.h 文件

#pragma once

//#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS      // some CString constructors will be explicit

#ifndef UNICODE
#define UNICODE
#endif

#ifndef _UNICODE
#define _UNICODE
#endif


using namespace std;


#ifndef TYPEDEF_H
 #define TYPEDEF_H

#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <tchar.h>
#include <time.h>



    //simple defines to allow the TCHAR library to be used
    typedef std::basic_string<TCHAR> tstring;
    typedef std::basic_ostream<TCHAR> tostream;
    typedef std::basic_istream<TCHAR> tistream;
    typedef std::basic_ostringstream<TCHAR> tostringstream;
    typedef std::basic_istringstream<TCHAR> tistringstream;
    typedef std::basic_ofstream<TCHAR>   tofstream;




    #if defined(UNICODE) || defined(_UNICODE)
        #define tcout std::wcout
        #define tcin std::wcin
    #else
        #define tcout std::cout
        #define tcin std::cin;
    #endif


 #endif

#if defined DEBUG || defined (_DEBUG)
#define TOCONSOLE
#endif


typedef enum LOGLVL{logERROR =0,logWARN,logINFO,logDEBUG};

//CLogBase os a singleton log class. Intent is that you can establish a reference from anywhere in the project and write to the same file 
// without having locking issues or threading issues

class CLogBase
{
public:
    static CLogBase& GetInstance(CString logname = L"log.txt",LOGLVL lvl = logWARN);
    ~CLogBase(void);

    tostringstream& GetLog(LOGLVL level);
    tostringstream& GetStream(LOGLVL);

    void Forceflush();


private:
    CLogBase(CString file,LOGLVL lvl);

    //our outstream

    tostringstream m_os;

    tostringstream m_dummy;

    tofstream m_filestream;

    CString m_filename;
    LOGLVL m_reportlvl;


    //Private declarations to prevent copy constructors from being invoked; these are do nothig implimentations 
    CLogBase(CLogBase const&);              
    void operator=(CLogBase const&); 


};


class CLog
{
public:
    CLog(CString component);
    CLog(CString component,CString logname,LOGLVL lvl);
    ~CLog();
    void Log(LOGLVL,CString message);
    void CLog::Flush();

    tostringstream& CLog::GetStream(LOGLVL lvl);


private:
    CString m_componentname;
    CLogBase* m_logBase;
};
4

1 回答 1

0

当我发现问题时,我想我会回答这个问题。这本身不是编码问题,而是视觉工作室问题。发生的事情是我将 direnum.h 文件和 .cpp 文件存储在与用于主项目的目录不同的目录中。在某个时间点使用 #include "..\somedir\direnum.h" 引用标头,Visual Studio 报告该文件已锁定,我是否要覆盖 \ 取消等。我选择覆盖,但似乎发生的是这导致VS 将文件复制到当前项目。我所有的故障排除尝试都在编辑器中打开的本地 somename.h 文件中进行编辑,但编译器正在做正确的事情并从上面的位置拉下 .h 文件。

删除切换到 direnum.h 的现在本地副本并重新编译修复了此问题,因为它将代码的一部分编译为 ANSI,另一部分编译为 WCHAR

于 2013-12-23T02:47:35.110 回答