2017-08-01 40 views
2

我對我的一個本地C++類中的「this」的NULL值感到困惑。C++中「this」的可能值是多少?

我聲明類Stdafx.h中,像這樣:

extern Filing* Files; 

然後實現它stdafx.cpp像這樣:

Filing* Files = new Filing(); 

調試器扼流圈這種方法,並拋出了「寫訪問衝突「: 消息還說:」這是nullptr「。

void Filing::Startup() 
{ 
this->ExePath = this->GetPathToExe(); //Throws right here 
this->LogDirectoryPath = this->GetLogDirectoryPath(); 
CreateDirectory(LogDirectoryPath->c_str(), NULL); 
} 

這怎麼可能?我該如何解決?完整的「歸檔」標題和下面的定義。

Filing.h:

#pragma once 

#include <Windows.h> 
#include <sstream> 

using namespace std; 

class Filing 
{ 
    public: 
    Filing(); 
    ~Filing(); 

/// <summary> 
/// The path to this exe. 
/// </summary> 
    wstring* ExePath; 

/// <summary> 
/// The path to the log folder that exists or will be created. 
/// </summary> 
    wstring* LogDirectoryPath; 

/// <summary> 
/// Kicks off some startup logic. 
/// </summary> 
    void Startup(); 

/// <summary> 
/// Determines if the specified directory exists. 
/// </summary> 
/// <param name="Path"></param> 
/// <returns></returns> 
    bool DoesDirectoryExist(string* Path); 

    private: 

/// <summary> 
/// Returns the directory from which this executable is running from. 
/// </summary> 
    wstring* GetPathToExe(); 

/// <summary> 
/// Gets the path to the log directory. 
/// </summary> 
/// <returns></returns> 
    wstring* GetLogDirectoryPath(); 
}; 

Filing.cpp:

#include "stdafx.h" 
#include "Filing.h" 

Filing::Filing() 
{ 
    this->Startup(); 
} 

Filing::~Filing() 
{ 
    delete this->ExePath; 
    delete this->LogDirectoryPath; 
} 

void Filing::Startup() 
{ 
    this->ExePath = this->GetPathToExe(); 
    this->LogDirectoryPath = this->GetLogDirectoryPath(); 
    CreateDirectory(LogDirectoryPath->c_str(), NULL); 
} 

bool Filing::DoesDirectoryExist(string* Path) 
{ 
    DWORD ftyp = GetFileAttributesA(Path->c_str()); 

    if (ftyp == INVALID_FILE_ATTRIBUTES) 
    { 
    Console->WriteLine("Invalid path!"); 
    return false; //something is wrong with your path! 
    } 

    if (ftyp & FILE_ATTRIBUTE_DIRECTORY) 
    { 
     return true; // this is a directory! 
    } 

    return false; // this is not a directory! 
} 

wstring* Filing::GetPathToExe() 
{ 
    #ifdef UNICODE 
    TCHAR ownPth[260]; 
    #else 
    char ownPth[MAX_Path]; 
    #endif // UNICODE 

    // Will contain exe path 
    HMODULE hModule = GetModuleHandle(NULL); 
    if (hModule != NULL) 
    { 
     // When passing NULL to GetModuleHandle, it returns handle of exe itself 
     GetModuleFileName(hModule, ownPth, (sizeof(ownPth))); 
     return new wstring(ownPth); 
    } 
    else 
    { 
     throw new exception("Error! NullPointerException!"); 
    } 
} 

wstring* Filing::GetLogDirectoryPath() 
{ 
    //if (this == nullptr) 
    //{ 
    // int i = 0; 
    //} 

    wstring *directory; 
    const size_t last_slash_idx = ExePath->rfind('\\'); 
    if (string::npos != last_slash_idx) 
    { 
     directory = new wstring(ExePath->substr(0, last_slash_idx)); 
     directory->append(L"\\Logs"); 
    } 
    else 
    { 
     throw new exception("Error! Directory not found from path!"); 
    } 
    return directory; 
} 

事情我至今嘗試過:

清理和重建。

初始化歸檔。

編輯:

我所看到的關於不使用的stdafx.h和stdafx.cpp什麼我使用它的一些意見。我現在正在將它們用於全局變量/類。我怎麼能在其他地方定義它們,仍然是全球性的?

EDIT2:

究其根源,錯誤一路回了一點GETTIME()函數。

const char* Writer::GetTime() 
{ 
    string* Formatted = new string("["); 
    time_t rawtime; 
    tm* timeinfo; 
    char buffer[80]; 
    time(&rawtime); 
    timeinfo = std::localtime(&rawtime); 

    strftime(buffer, 80, "%Y-%m-%d-%H-%M-%S", timeinfo); 
    Formatted->append(buffer); 
    Formatted->append("]: "); 

    const char* Ret = Formatted->c_str(); 

    delete Formatted; 
    delete timeinfo;//Errors here SOMETIMES. Any ideas? 
    return Ret; 
} 
+0

我不在Winders中編程,但是你應該編輯stdafx.h?我以爲這是編譯器生成的預編譯頭文件?但是,那麼我對Winders有什麼瞭解? – systemcpro

+0

@systemcpro:它由IDE生成。但你可以自由編輯它。 –

+0

爲什麼這個== nullptr不允許?這不是在我的原始代碼中的錯誤,它只是調試代碼,以證明錯誤沒有被調試器誤診。無論如何,如果你刪除它,你仍然會遇到問題。 – SneakyTactician2

回答

3

這裏有一個程序,可以結果在你看到this設置爲NULL指針的例子。 (我說可能,因爲根據C++語言規範,這個程序調用未定義的行爲,所以在技術上編譯器可以自由地崩潰或輸出一個PacMan的可執行文件或任何它想要做的響應這個源代碼......但在許多編譯器實現,一個通常觀察到的行爲是一個NULL這個指針咳咳):

#include <stdio.h> 

class Foo 
{ 
public: 
    Foo() {/* empty*/} 

    void bar() {printf("this=%p\n", this);} 
}; 

int main(int, char **) 
{ 
    Foo * f = NULL; 
    f->bar(); // undefined behavior here: calling a method on a NULL object! 
    return 0; 
} 

當我運行這個程序,我得到這樣的輸出:

$ ./a.out 
this=0x0 

不用說,你不應該在任何真正的程序中這樣做。

至於你自己的代碼,最可能的解釋是你正在調用NULL指針的Startup()方法。解決方法是跟蹤所有調用Startup()的位置,並確保在調用其上的任何方法之前,它們調用Startup()的指針是非NULL(並指向有效對象)。

相關問題