2013-12-19 60 views
0

所以把這個應用程序放在一起我已經開始在調用cstring類成員的析構函數時獲得運行時檢查失敗堆棧損壞。 我已經到了試圖通過在問題上投擲磚塊來調試這一點,但仍然沒有根源造成它。在當前時刻,cstring駐留的類什麼都不做,只是初始化它的私有字符串成員並將指向另一個類的指針設置爲NULL。將Class ptr設置爲NULL導致CString析構函數運行時檢查失敗?

有趣的是,如果我沒有將類指針設置爲NULL並且將該行註釋掉,則損壞消失。我認爲這是一個紅鯡魚,並且當編譯器將代碼放入包含theCLog定義的.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; 
}; 
+0

您的堆棧腐敗是一個「計算器」的這個,因爲它是在編譯的一部分? – manuell

回答

0

我以爲我會回答這個,因爲我發現這個問題。這本身不是一個編碼問題,而是一個視覺工作室問題。 發生了什麼事情是我將direnum.h文件和.cpp文件存儲在與主項目不同的目錄中。用#include「.. \ somedir \ direnum.h」引用標題,在一個時間點visual studio報告文件被鎖定,我想覆蓋\取消等。我選擇覆蓋,但似乎發生的是這個導致VS將文件複製到當前項目。我所有的故障排除嘗試都是在編輯器中打開的本地somename.h文件中編輯的,但編譯器正在做正確的事情並從上面的位置拉下.h文件。

消除切換到direnum.h的現在本地副本,並重新編譯固定的代碼爲ANSI,另一部分作爲WCHAR

相關問題