无法读取内存以删除对象数组

时间:2016-04-05 15:55:44

标签: c++ class oop object dynamic-memory-allocation

所以我在C ++中测试一些对象数组,之后我试图删除这些对象,就像我应该的那样。

但问题是:deleteInputPattern变量工作正常,所以我能够完全删除CSVFile头类中的“输入”,但它在主文件“inputArray”中的等效项会触发断点。

这是什么问题?我想删除不存在的内存吗?任何指针都需要

下面的代码墙:

InputTest.h:

#pragma once
class InputTest
{
    private:
        float r;
        float g;
        float b;
        float t;
    public:
        InputTest();
        ~InputTest();
        InputTest(float r, float g, float b, float t);

        void setR(float newT);
        float getR();
        void setG(float newT);
        float getG();
        void setB(float newT);
        float getB();
        void setT(float newT);
        float getT();

        void print(int count);
};

InputTest.cpp:

#include "InputTest.h"
#include <stdio.h>

InputTest::InputTest()
{
    printf("Input constructor\n");
}

InputTest::~InputTest()
{
    printf("Input destructor\n");
}

InputTest::InputTest(float r, float g, float b, float t)
{
    this->r = r;
    this->g = g;
    this->b = b;
    this->t = t;
}

void InputTest::setR(float newT)
{
    r = newT;
}

float InputTest::getR()
{
    return r;
}

void InputTest::setG(float newT)
{
    g = newT;
}

float InputTest::getG()
{
    return g;
}

void InputTest::setB(float newT)
{
    b = newT;
}

float InputTest::getB()
{
    return b;
}

void InputTest::setT(float newT)
{
    t = newT;
}

float InputTest::getT()
{
    return t;
}

void InputTest::print(int count)
{
    printf("R: %.2f\n", r);
    printf("G: %.2f\n", g);
    printf("B: %.2f\n", b);
    printf("T: %.2f\n", t);
}

Copy.h:

#pragma once
class InputTest;

class Copy
{
    private:
        int patternCount;
        InputTest** inputs;

    public:
        Copy();
        ~Copy();

        InputTest* getInputPattern(int index);
        void addInputPattern(InputTest* in);
        void deleteInputPattern();
};

Copy.cpp:

#include "Copy.h"
#include "InputTest.h"
#include <string.h>
#include <stdio.h>

Copy::Copy()
{
    printf("CSV File constructor\n");
    inputs = NULL;
    patternCount = 0;

    inputs = new InputTest*[3];
    int i;
    for (i = 0; i < 3; i++)
    {
        inputs[i] = new InputTest();
    }
}

Copy::~Copy()
{
    printf("CSV File destructor\n");
}

InputTest * Copy::getInputPattern(int index)
{
    printf("input gotten: %d\n", index);
    return inputs[index];
}

void Copy::addInputPattern(InputTest * in)
{
    inputs[patternCount] = in;
    patternCount++;
    printf("input added: %d\n", patternCount);
}

void Copy::deleteInputPattern()
{
    int i;
    for (i = 0; i < patternCount; i++)
    {
        delete inputs[i];
    }
    delete inputs;
    inputs = NULL;
}

main.cpp中:

#include "Copy.h"
#include "InputTest.h"
#include <string.h>
#include <stdio.h>

int main(int argv, char** argc)
{
    bool testResult = false;
    Copy *test = NULL;
    test = new Copy();

    InputTest **inputArray;
    inputArray = new InputTest*[3];
    int count;

    for (count = 0; count < 3; count++)
    {
        inputArray[count] = new InputTest();

        inputArray[count]->setR(0.2f);
        inputArray[count]->setG(0.6f);
        inputArray[count]->setB(0.8f);
        inputArray[count]->setT(0.5f);

        test->addInputPattern(inputArray[count]);
        inputArray[count] = test->getInputPattern(count);
        printf("next\n");
    }

    for (count = 0; count < 3; count++)
    {
        printf("round %d\n", count);
        printf("R: %f\n", inputArray[count]->getR());
        printf("G: %f\n", inputArray[count]->getG());
        printf("B: %f\n", inputArray[count]->getB());
        printf("T: %f\n", inputArray[count]->getT());
    }

    test->deleteInputPattern();

    for (count = 0; count < 3; count++)
    {
        delete inputArray[count];
    }

    delete inputArray;
    delete test;
    inputArray = NULL;
    test = NULL;
    return testResult;
}

1 个答案:

答案 0 :(得分:0)

这些似乎是有问题的一行:

test->deleteInputPattern();

for (count = 0; count < 3; count++)
{
    delete inputArray[count];
}

由于您已使用test->deleteInputPattern()删除了该内存,因此将释放该内存。现在,您将使用delete inputArray在main中显式删除相同的对象(您仍然通过inputArray持有引用)。但是该内存已在deleteInputPattern中删除,因此您应该收到内存访问错误。

您只需要释放一次已分配的内存。没有必要在main()中再次执行此操作。在main中调用deleteInputPattern或显式调用delete,但不能同时调用两者。我可以推荐2个最佳实践:

  1. 使用智能指针
  2. 分配模块应该删除内存(这可能不适用于很多情况,例如工厂)