C++ std::vector of pointers 删除和分段错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/891913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:48:30  来源:igfitidea点击:

C++ std::vector of pointers deletion and segmentation faults

c++pointersmemory-managementsegmentation-fault

提问by Lefteris

I have a vector of pointers to a class. I need to call their destructors and free their memory. Since they are vector of pointers vector.clear() does not do the job.So I went on to do it manually like so :

我有一个指向类的指针向量。我需要调用它们的析构函数并释放它们的内存。因为它们是指针向量,所以 vector.clear() 不能完成这项工作。所以我继续像这样手动完成:

void Population::clearPool(std::vector<Chromosome*> a,int size)
{
    Chromosome* c;
    for(int j = 0 ;j < size-1;j++)
    {
       c = a.back();
       a.pop_back();
       delete c;
       printf("  %d \n\r",j);
       c = NULL;

    }

}

The printf in there is since I have a talking destructor to see in which Chromosome the segmentation fault happens. When clearPool() is called and say we got a size of 100, it can give a segmentation fault in any Chromosome between 0 and 100.

那里的 printf 是因为我有一个会说话的析构函数来查看分段错误发生在哪个染色体中。当调用 clearPool() 并说我们的大小为 100 时,它会在 0 到 100 之间的任何染色体中给出分段错误。

I have no idea why this might be happening nor do I have a way to actually find what's wrong since while debugging with breakpoints all I see is that it happens in there at random chromosomes.

我不知道为什么会发生这种情况,也没有办法真正找出问题所在,因为在使用断点进行调试时,我所看到的只是它发生在随机染色体上。

I am using codeblocks IDE and the gdb debugger. The stack trace when the segmentation fault happens has 4 memory addresses and a function wsncpy().

我正在使用代码块 IDE 和 gdb 调试器。发生分段错误时的堆栈跟踪有 4 个内存地址和一个函数wsncpy()

回答by

void Population::clearPool( std::vector <Chromosome*> & a )
{
   for ( int i = 0; i < a.size(); i++ ) {
      delete a[i];
   }
   a.clear();
}

Notice that the vector is passed by reference. In your code, a copy of the vector is used, which means that it is unchanged in the calling program. Because you delete the pointers in the copy, the pointers in the original are now all invalid - I suspect you are using those invalid pointers in some way not shown in the code you posted.

请注意,向量是通过引用传递的。在您的代码中,使用了向量的副本,这意味着它在调用程序中没有改变。因为您删除了副本中的指针,所以原始中的指针现在全部无效 - 我怀疑您正在以某种方式使用这些无效指针,而您发布的代码中并未显示这些指针。

As a couple of template solutions have been posted that use C++ library algorithms, you might also want to consider a template solution that does not:

由于已经发布了几个使用 C++ 库算法的模板解决方案,您可能还需要考虑一个不使用的模板解决方案:

template <class C> void FreeClear( C & cntr ) {
    for ( typename C::iterator it = cntr.begin(); 
              it != cntr.end(); ++it ) {
        delete * it;
    }
    cntr.clear();
}

Using this you can free any container of dynamically allocated objects:

使用它,您可以释放任何动态分配对象的容器:

vector <Chromosome *> vc;
list <Chromosome *> lc;
// populate & use
FreeClear( lc );
FreeClear( vc );

回答by aJ.

Slight modified version compared to (@1800 INFORMATION).

与 (@1800 INFORMATION) 相比略有修改的版本。

  struct DeleteFromVector
    {
        template <class T>
        void operator() ( T* ptr) const
        {
            delete ptr;
        }
    };


std::for_each(aVec.begin(), aVec.end(), DeleteFromVector());

回答by James Hopkin

Boost lambda already has a functor for deleting sequences of pointers, by the way:

顺便说一下,Boost lambda 已经有一个用于删除指针序列的函子:

std::for_each(a.begin(), a.end(), boost::lambda::delete_ptr());

回答by 1800 INFORMATION

I don't know why you are crashing, but I guess that one possibility is that the size of the vector is not the same as the size you are passing in. Also I notice you are iterating from 0 to size-2, do you not mean to go all the way to the end?

我不知道你为什么会崩溃,但我想一种可能性是向量的大小与你传入的大小不同。另外我注意到你正在从 0 迭代到大小 2,是吗?不是要一路走到最后吗?

One way to delete all of the items in the array using idiomatic C++ is something like this:

使用惯用 C++ 删除数组中所有项目的一种方法是这样的:

template<class T>
class deleter
{
  public:
    void operator()(const T* it) const
    {
      delete it;
    }
};

std::for_each(a.begin(), a.end(), deleter<Chromosome>());

回答by Binary Worrier

Are you sure that each pointer in the vector points to a different object? (i.e. that two pointers don't both point to the same object, which you're trying to delete twice.

您确定向量中的每个指针都指向不同的对象吗?(即,两个指针并不都指向同一个对象,您试图删除该对象两次。

Are you sure that you don't delete some of the pointers before calling this method? (i.e. are you sure that each pointer in the list points to a valid object?)

你确定你在调用这个方法之前没有删除一些指针吗?(即您确定列表中的每个指针都指向一个有效的对象吗?)

回答by sharptooth

The most likely reason is calling deletetwice for the same address. This can happen if you added one object more than once to the vector. To detect this insert some statement that will output the address of the object you will then delete.

最可能的原因是为同一个地址调用delete两次。如果您多次向矢量添加一个对象,就会发生这种情况。要检测此插入一些语句,该语句将输出您将删除的对象的地址。

printf( "will delete %d\n", (int)c );
delete c;

回答by Lefteris

I found the problem.

我发现了问题。

It was in the most well hidden (by none other than stupid old me) place it could be.

它在它可能隐藏的最深的地方(除了愚蠢的老我之外)。

As some might have guessed this is a genetic algorithms program. It is for a tutorial I am making. I was choosing the crossover points for the chromosomes randomly from a roulette wheel function which I made. Well ... inside there, there was a -1 which should not be there. That destroyed literally everything, and eventually lead to a segmentation fault.

有些人可能已经猜到,这是一个遗传算法程序。这是我正在制作的教程。我从我制作的轮盘赌函数中随机选择染色体的交叉点。嗯......在那里,有一个 -1 不应该在那里。这实际上摧毁了一切,并最终导致分段错误。

Thank you all for your help, I saw some really good practises in this post which I intend to follow

谢谢大家的帮助,我在这篇文章中看到了一些非常好的做法,我打算遵循

回答by Ahmed Said

I recommend to use smart pointer (ie:auto_ptr) instead of raw pointer and just use vector::clear method that will call the destructor for each element

我建议使用智能指针(即:auto_ptr)而不是原始指针,只需使用 vector::clear 方法,该方法将为每个元素调用析构函数

回答by Konstantin Tenzin

It seems, that some pointers in your code do not reference correct Chromosome objects. This may happen, if you try to delete some objects twice as a result of code:

代码中的某些指针似乎没有引用正确的 Chromosome 对象。如果由于代码而尝试两次删除某些对象,则可能会发生这种情况:

Population p;
vector<Chromosome*> chromosomes;
p.clearPool(chromosomes,chromosomes.size()); // You pass by value, so chromosomes is not changed
p.clearPool(chromosomes,chromosomes.size()); // Delete already deleted objects second time

You may find useful ptr_vector from Boost Pointer Container Libraryin order to avoid similar errors

您可能会从Boost Pointer Container Library中找到有用的 ptr_vector以避免类似的错误

回答by Daniel

void Population::clearPool(std::vector<Chromosome*>& a)
{
    for(size_t i = 0; i < a.size(); i++) {
        delete a[i];
    }

    a.clear();
}