首页 > 解决方案 > 在 0x0037A5C2 project.exe 处抛出异常:0xC0000005:程序末尾的访问冲突写入位置 0xDDDDDDDD

问题描述

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

源.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;
    }

处理动态内存分配的唯一设置器如下:

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++constructordestructorcopy-constructormemcpy

解决方案


快速浏览一下,我会说问题出在您的setColumnssetCommandData功能上。(我当然可能是错的,我没有尝试运行您提供的代码,也没有尝试运行我所做的更改——因此某处也可能存在拼写错误。)

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

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

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

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,但如果您仍在学习,我建议您暂时不要这样做。你很快就会到达那里。


推荐阅读