c++正确删除结构和指针

标签 c++ pointers memory struct garbage-collection

我有一个关于如何正确删除结构及其内部声明的相应指针的问题。

我从我正在运行的项目中提取了一个示例,但它似乎无法正常工作,代码没有崩溃,但似乎我有一些“内存泄漏”。我不确定这是正确的措辞。问题是这些值并没有真正重置,而是在我下次开始上课时保存在内存中。

下面的Sudocode:

标题:

ProgramHeader.h

class ClassA : public publicClassA
{
    public:
        ClassA(void);

        virtual ~ClassA();

    private:

    struct ApStruct{
            struct
            {
                float *refA[2];
                float *refB[2];
                float *pVarA;
            } fR;

            struct
            {
                float *refA[2];
                float *refB[2];
                float *pVarA;
            } f1kHz;
        };
        ApStruct* GetApStruct;  
}

程序:

Program.cpp

#include "ProgramHeader.h"

ClassA::~ClassA()
{
    //EDIT i did a typo my looks like this:
    //delete ApStruct; //Wrong code
    delete GetApStruct; //Corrected - however still not working
}

main()
{
    GetApStruct  = new ApStruct();

    //Do Code
}

希望这一切都有意义,

编辑: 我更新了代码中的一行错误 - 但是问题仍然存在。在我实现解决方案之前,我会先看看下面的内容以了解情况。

编辑 24/10/2015 我一直在尝试下面的一些建议,但我无法找到解决我的问题的方法,我必须承认我也很难缩小可能导致问题的原因。

我的代码是 DLL 的一部分。该代码包含一些我无法控制的源代码,因此我在如何使用构造函数和指针上进行初始化方面的选择有限。

我仍然认为我有内存泄漏问题的原因是,如果我在我的代码中添加一个“魔法 float ”,我的函数的输出就会改变,即使 float 不是 在任何地方使用 - 它只是被声明。

我在以下情况下会得到不同的结果:

  1. 调用 InitCode - 一次!
  2. 然后我将多次调用 CallCode - 进行计算
  3. 销毁类的实例

当我再次重复上述操作时,我得到的结果与我第一次运行代码时不同,但之后它保持不变。

如果我包含魔法线,一切似乎都有效???

更新的 SudoCode:

Program.cpp

#include "ProgramHeader.h"

ClassA::~ClassA()
{
    //EDIT i did a typo my looks like this:
    //delete ApStruct; //Wrong code
    delete GetApStruct; //Corrected - however still not working
}

main()
{
    void initCode()
    {
        GetApStruct  = new ApStruct();

        float InitValue = 0.F

        //Magic line:
        float magicLine = 123456.f; //If this line is commented out i get different results in my code
        //End Magic Line

        fr.refA[0] = &InitValue;
        fr.refA[0] = &InitValue;
        fr.refA[0] = &InitValue;
        fr.pVarA   = &InitValue;
        ... 
    }

    void CallCode()
    {
        float CallValue = 123.F

        //Magic line:
        float magicLine = 123456.f; //If this line is commented out i get different results in my code
        //End Magic Line

        fr.refA[0] = &CallValue;
        fr.refA[0] = &CallValue;
        fr.refA[0] = &CallValue;
        fr.pVarA   = &CallValue;
        ...
    }
}

谢谢大家的支持

托马斯

最佳答案

我会推荐类似下面的分配和清理...

#include <iostream>

using namespace std;

class ClassA 
{
public:
    ClassA(void);
    virtual ~ClassA();

private:
    struct ApStruct {
        struct
        {
            float *refA[2];
            float *refB[2];
            float *pVarA;
        } fR;

        struct
        {
            float *refA[2];
            float *refB[2];
            float *pVarA;
        } f1kHz;
    };
    ApStruct* GetApStruct;
};

ClassA::ClassA(void) {
    GetApStruct = new ApStruct{};
    GetApStruct->fR.refA[0] = new float{ 1.f };
    GetApStruct->fR.refA[1] = new float{ 2.f };
    GetApStruct->fR.refB[0] = new float{ 3.f };
    GetApStruct->fR.refB[1] = new float{ 4.f };
    GetApStruct->fR.pVarA = new float { 0.f };
    // do same for struct f1kHz
    // ...
    cout << "Construction" << endl;
}

ClassA::~ClassA()
{
    if (GetApStruct != nullptr) {
        if (GetApStruct->fR.refA[0] != nullptr) {
            delete GetApStruct->fR.refA[0];
            GetApStruct->fR.refA[0] = nullptr;
        }
        if (GetApStruct->fR.refA[1] != nullptr) {
            delete GetApStruct->fR.refA[1];
            GetApStruct->fR.refA[1] = nullptr;
        }
        if (GetApStruct->fR.refB[0] != nullptr) {
            delete GetApStruct->fR.refB[0];
            GetApStruct->fR.refB[0] = nullptr;
        }
        if (GetApStruct->fR.refB[1] != nullptr) {
            delete GetApStruct->fR.refB[1];
            GetApStruct->fR.refB[1] = nullptr;
        }
        if (GetApStruct->fR.pVarA != nullptr) {
            delete GetApStruct->fR.pVarA;
            GetApStruct->fR.pVarA = nullptr;
        }
        // do same for struct f1kHz
        // ...
        // finally
        delete GetApStruct;
        GetApStruct = nullptr;
    }
    cout << "Destruction" << endl;
}

int main() {
    {
        ClassA a;
    }

    system("pause");
    return 0;
}

关于c++正确删除结构和指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33275816/

相关文章:

c++ - 内存访问冲突的操作系统处理与内存写入冲突的处理

c++ - 如何将逗号分隔的数字矩阵放入 C++ 中的动态分配数组中?

c - 字符数组的状态

javascript - innerHTML 是否在 DOM 树中创建节点?

c++ - 使用 std::less 创建一个 std::map 环绕原点

c++ - GetOpenFileName 函数未打开对话框

c - 双指针 vs 指针数组(**array vs *array[])

java - 垃圾收集器如何快速知道哪些对象不再有对它们的引用?

c - 何时释放 mex 文件中分配的内存

java - C++ 和 Java 有哪些记录和重放网络协议(protocol) stub /模拟工具?