简体   繁体   English

对删除分配给结构数组的动态内存感到困惑

[英]Confused about deleting dynamic memory allocated to array of struct

I'm having a memory leak issue and it's related to an array of structs inside a class (not sure if it matters that they're in a class). 我遇到了内存泄漏问题,它与类中的一系列结构有关(不确定它们是否在类中很重要)。 When I call delete on the struct, the memory is not cleared. 当我在该结构上调用delete时,不会清除内存。 When I use the exact same process with int and dbl it works fine and frees the memory as it should. 当我对int和dbl使用完全相同的进程时,它可以正常工作并释放内存。

I've created very simple examples and they work correctly so it's related to something else in the code but I'm not sure what that could be. 我已经创建了非常简单的示例,并且它们可以正常工作,因此与代码中的其他内容有关,但是我不确定这可能是什么。 I never get any errors and the code executes correctly. 我从来没有收到任何错误,并且代码可以正确执行。 However, the allocation / deallocation occurs in a loop so the memory usage continually rises. 但是,分配/释放是在一个循环中发生的,因此内存使用量不断增加。

In other words, here's a summary of the problem: 换句话说,这是问题的摘要:

struct myBogusStruct {
    int bogusInt1, bogusInt2;
};

class myBogusClass {
    public:
       myBogusStruct *bogusStruct;
};

void main(void) {

    int i, arraySize;
    double *bogusDbl;
    myBogusClass bogusClass;

    // arraySize is read in from an input file

    for(i=0;i<100;i++) {
        bogusDbl = new double[arraySize];
        bogusClass.bogusStruct = new myBogusStruct[arraySize];

        // bunch of other code

        delete [] bogusDbl; // this frees memory
        delete [] bogusClass.bogusStruct; // this does not free memory
    }
 }

When I remove the bunch of other code, both delete lines work correctly. 当我删除一堆其他代码时,两个删除行均正常工作。 When it's there, though, the second delete line does nothing. 但是,当它在那里时,第二条删除行不执行任何操作。 Again, I never get any errors from the code, just memory leaks. 再说一次,我从代码中从未得到任何错误,只是内存泄漏。 Also, if I replace arraySize with a fixed number like 5000 then both delete lines works correctly. 另外,如果我用固定的数字(例如5000)替换arraySize,那么两个删除行都可以正常工作。

I'm not really sure where to start looking - what could possibly cause the delete line not to work? 我不确定从哪里开始寻找内容-什么可能导致删除行无法正常工作?

There is no reason at all for you to either allocate or delete myBogusDbl inside the for loop, because arraySize never changes inside the loop. 没有任何理由可言让你无论是分配或删除myBogusDbl里面的for循环,因为arraySize从来没有在循环内改变。

Same goes for myBogusClass.myBogusStruct . myBogusClass.myBogusStruct No reason to allocate/delete it at all inside the loop: 没有理由在循环内完全分配/删除它:

myBogusDbl = new double[arraySize];
myBogusClass.myBogusStruct = new bogusStruct[arraySize];

for (i = 0; i < 100; i++) {
    // bunch of other code
}

delete[] myBogusDbl;
delete[] myBogusClass.myBogusStruct;

You should also consider using std::vector instead of using raw memory allocation. 您还应该考虑使用std::vector而不是使用原始内存分配。

Now to the possible reason of why the second delete in the original code doesn't do anything: deleting a NULL pointer does, by definition, nothing. 现在来说明为什么原始代码中的第二个删除不执行任何操作的可能原因:按照定义,删除NULL指针不会执行任何操作。 It's a no-op. 这是没有操作的。 So for debugging purposes, try introducing a test before deleting it to see if it's NULL and if yes abort(). 因此,出于调试目的,请尝试在删除测试之前引入测试,以查看它是否为NULL,如果是,则中止()。 (I'd use a debugger instead though, as it's much quicker to set up a watch expression there compared to writing debug code.) (不过我会改用调试器,因为与编写调试代码相比,在其中设置监视表达式要快得多。)

In general though, we need to see that "bunch of other code". 总的来说,我们需要看到“其他代码束”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM