gpt4 book ai didi

c++ - C++ 中 "this"的可能值是 Null 吗?

转载 作者:搜寻专家 更新时间:2023-10-31 01:31:35 24 4
gpt4 key购买 nike

<分区>

我对我的一个本地 C++ 类中“this”的 NULL 值感到困惑。

我在 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);
}

这怎么可能?我该如何解决?完整的“归档”标题和定义如下。

归档.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();
};

归档.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 的评论。我现在将它们用于全局变量/类。我如何在别处定义它们并且仍然是全局的?

编辑2:

一直将错误追溯到一个小的 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;
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com