0x0037A5C2 project.exe时引发异常: 0xC0000005:访问冲突写入程序末尾的位置0xDDDDDDDD

Exception thrown at 0x0037A5C2 project.exe: 0xC0000005: Access violation writing location 0xDDDDDDDD at the end of the program

提问人:Polly 提问时间:11/24/2020 最后编辑:Polly 更新时间:11/24/2020 访问量:1014

问:

我在程序的最后遇到了这个运行时异常,只需创建指定类的实例,所以我认为问题出在构造函数、复制构造函数、复制赋值运算符或析构函数上。在我有限的 cpp 知识范围内,我已经阅读并遵循了三法则。

Source.cpp

#include "Header.h"
#include <iostream>

using namespace std;


int main() {


string command = "CREATE TABLE table_name IF NOT EXISTS ((column_1_name,type,default_value), (column_2_name,type,default_value))";
string columns[20] = { "column_1_name,type,default_value", "column_1_name,type,default_value" };
string commandData[9] = { "table_name", "IF NOT EXISTS" };

CommCREATETABLE comm(command, columns, commandData, 2, 2);

}

来自 Header.h 的相关代码

class CommCREATETABLE {
    string fullCommand = "";
    string* columns = nullptr;
    string* commandData = nullptr;
    string tableName = "";
    int nrOfColumns = 0;
    int nrOfElements = 0;
    bool valid = false;

构造 函数:

CommCREATETABLE(string command, string* columns, string* commandData, int nrOfRows, int nrOfElements) {
        this->setNrOfColumns(nrOfRows);
        this->setNrOfElements(nrOfElements);
        this->setCommand(command);
        this->setColumns(columns);
        this->setCommandData(commandData);
        this->valid = checkInput(this->commandData, this->columns);
        this->setTableName(commandData[0]);
    }

复制构造函数、复制赋值运算符、析构函数:

CommCREATETABLE(const CommCREATETABLE& comm) {
        this->setNrOfColumns(comm.nrOfColumns);
        this->setNrOfElements(comm.nrOfElements);
        this->setCommand(comm.fullCommand);
        this->setColumns(comm.columns);
        this->setCommandData(comm.commandData);
        this->setTableName(comm.tableName);
        this->valid = comm.valid;
    }
    ~CommCREATETABLE() {
        if (this->columns != nullptr) {
            delete[] this->columns;
        }
        if (this->commandData != nullptr) {
            delete[] this->commandData;
        }
    }

    CommCREATETABLE& operator=(const CommCREATETABLE& comm) {
        this->setCommand(comm.fullCommand);
        this->setColumns(comm.columns);
        this->setCommandData(comm.commandData);
        this->setTableName(comm.tableName);
        this->setNrOfColumns(comm.nrOfColumns);
        this->setNrOfElements(comm.nrOfElements);
        this->valid = checkInput(this->commandData, this->columns);
        return *this;
    }

处理动态内存分配的唯一 setter 如下:

void setColumns(const string* columns) {
        if (this->nrOfColumns >= 0) {
            this->columns = new string[this->nrOfColumns];
            memcpy(this->columns, columns, this->nrOfColumns * sizeof(string));
        }
        else throw EmptyCommandException();
    }
    void setCommandData(const string* commandData) {
        if (this->nrOfElements >= 0) {
            this->commandData = new string[this->nrOfElements];
            memcpy(this->commandData, commandData, this->nrOfElements * sizeof(string));
        }
        else throw EmptyCommandException();
    }
C++ 析构函数 复制 构造函数 MEMCPY

评论

5赞 drescherjm 11/24/2020
Visual Studio 中的0xDDDDDDDD意味着要取消引用指向已释放的堆内存的指针:https://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations
3赞 Qubit 11/24/2020
我会避免像你现在这样使用(最后一对方法)。在内部,类包含一个指针,您现在正在做的是复制该指针。这意味着,当原始字符串被删除时,指针将不再有效。但是,您的类仍然保留相同的指针。因此,当您的类被删除时,它会再次尝试删除该指针。这显然会引起问题。相反,只需通过遍历所有字符串来复制数组中的每个字符串,即可解决问题。memcpystring=

答:

2赞 Qubit 11/24/2020 #1

乍一看,我会说问题出在您的和功能上。(我当然可能是错的,我没有尝试运行你提供的代码,也没有尝试运行我所做的更改——所以也可能在某处有错别字。setColumnssetCommandData

在那里,你用来将字符串复制到你的类中。但是,在内部,C++ 包含指向实际字符串的指针,因此实际上只复制该指针。因此,一旦原始字符串被删除,复制到类中的指针将不再有效(因为内存已被释放)。因此,一旦您的类也被删除,它就会尝试删除已释放的内存。这可能就是你的错误的来源。memcpystringmemcpy

事实上,如果您在程序中添加了试图操作类的行(在原始输入字符串已被删除之后),问题会更快出现,因为您将访问已经释放的内存。这将导致未定义的行为,通常在某个时候以崩溃告终。

一个快速的解决方法是更改复制数据的方式,方法是使用 for 每个字符串(以这种方式将实际字符串复制到内存中的新位置,而不是复制指针)。=

void setColumns(const string* columns) {
    if (this->nrOfColumns > 0) { // Creating an array of size 0 is also not a good idea. 
        this->columns = new string[this->nrOfColumns];
        for (int i = 0; i < nrOfColumns; i++) { // You don't need this everywhere. 
            this->columns[i] = columns[i]; 
            // I don't think naming things the exact same way is good practice. 
        }
    }
    else throw EmptyCommandException();
}
void setCommandData(const string* commandData) {
    if (this->nrOfElements > 0) { // Creating an array of size 0 is also not a good idea. 
        this->commandData = new string[this->nrOfElements];
        for (int i = 0; i < nrOfElements; i++) { // You don't need this everywhere. 
            this->commandData[i] = commandData[i]; 
            // I don't think naming things the exact same way is good practice. 
        }
    }
    else throw EmptyCommandException();
}

或者,如果你想避免制作副本,你应该研究一下,但如果你还在学习,我建议暂时不要这样做。你很快就会到达那里。move

评论

2赞 user4581301 11/24/2020
泛化:仅当被复制的对象是 Trivially Copyabvle 时才能使用memcpy
1赞 user4581301 11/24/2020
旁注:如果允许,请使用 std::vector 而不是动态分配的数组。节省时间,通常节省大量调试。
2赞 Blastfurnace 11/24/2020
最好用 std::copy 而不是循环替换 any。在这种情况下,你会得到正确的行为,并且对于微不足道的可复制类型,它可能会编译成一个快速的 .memcpyformemmove