简体   繁体   English

我是否正确删除了指向对象的指针矢量?

[英]Am I deleting my vector of pointers to objects correctly?

i am currently working on a programm, where i read Information from various CAN signals and store them in 3 different vectors. 我目前正在编程,我从各种CAN信号中读取信息,并将其存储在3个不同的向量中。 For each signal, a new pointer to an object is created an stored in two vectors. 对于每个信号,将创建一个指向对象的新指针,并将其存储在两个向量中。 My question is, when i am deleting the vectors, is it enough, when i delete the pointers to the objects in one vector and just clear the other one. 我的问题是,当我删除向量时,是否足够,当我删除一个向量中指向对象的指针并清除另一个向量时。

Here is my code: 这是我的代码:

Declaration of vectors an iterator: 向量声明的迭代器:

std::vector <CAN_Signal*> can_signals; ///< Stores all CAN signals, found in the csv file
std::vector <CAN_Signal*> can_signals_rx; ///< Stores only the Rx CAN signals, found in the csv file
std::vector <CAN_Signal*> can_signals_tx; ///< Stores only the Tx CAN signals, found in the csv file
std::vector <CAN_Signal*>::iterator signal_iterator; ///< Iterator for iterating through the varoius vectors

Filling the vectors: 填充向量:

for (unsigned int i = 0; i < m_number_of_lines; ++i)
{
    string s = csv_file.get_line(i);

    CAN_Signal* can_signal = new CAN_Signal(s, i);

    if (can_signal->read_line() == false)
        return false;

    can_signal->generate_data();

    can_signals.push_back(can_signal);

    if (get_first_character(can_signal->get_PDOName()) == 'R')
    {
        can_signals_rx.push_back(can_signal);
    }
    else if (get_first_character(can_signal->get_PDOName()) == 'T')
    {
        can_signals_tx.push_back(can_signal);
    }
    else
    {
        cout << "Error! Unable to detect whether signal direction is Rx or Tx!" << endl;
        return false;
    }   
}

Deleting the vectors: 删除向量:

File_Output::~File_Output()
{
    for (signal_iterator = can_signals.begin(); signal_iterator != can_signals.end(); ++signal_iterator)
    {
        delete (*signal_iterator);
    }
can_signals.clear();

//for (signal_iterator = can_signals_rx.begin(); signal_iterator != can_signals_rx.end(); ++signal_iterator)
//{
//  delete (*signal_iterator);
//}
can_signals_rx.clear();

//for (signal_iterator = can_signals_tx.begin(); signal_iterator != can_signals_tx.end(); ++signal_iterator)
//{
//  delete (*signal_iterator);
//}
can_signals_tx.clear();

cout << "Destructor File_Output!" << endl;
}

When i uncomment the commented for-loops and run the Programm, it crashes, when the destructor is called. 当我取消注释注释的for循环并运行Programm时,调用析构函数时它将崩溃。 So my guess is, that this is the correct way of doing so, because the pointers are allready deleted and it is enough to just clear remaining two vectors. 所以我的猜测是,这是正确的方法,因为指针已经全部删除,并且足以清除其余两个向量。

But i am not really sure and would really like to hear the opinion of an expert on this. 但是我不确定,也很想听听专家对此的意见。

Thank you very much. 非常感谢你。

when i am deleting the vectors, is it enough, when i delete the pointers to the objects in one vector and just clear the other one. 当我删除矢量时,是否足够,当我删除一个矢量中指向对象的指针并清除另一矢量时,就足够了。

Since the pointers in the other vectors are copies, not only is it sufficient to delete them only in the one vector, but deleting the copies would in fact have undefined behaviour. 由于其他向量中的指针是副本,因此仅在一个向量中删除它们不仅足够,而且删除副本实际上将具有不确定的行为。 You don't ever want your program to have undefined behaviour. 您永远都不希望您的程序具有未定义的行为。

Clearing any of the vectors in the destructor of File_Output seems to be unnecessary, assuming the vectors are members of File_Output . 清除任何在的析构函数矢量的File_Output似乎是不必要的,假设矢量是成员File_Output This is because the members are about to be destroyed anyway. 这是因为这些成员无论如何都将被销毁。

Am I deleting my vector of pointers to objects correctly? 我是否正确删除了指向对象的指针矢量?

With the assumption that you didn't make copies of the pointers that you delete elsewhere: Yes, that is the correct way to delete them. 假定您没有复制要在其他地方删除的指针的副本:是的,这是删除指针的正确方法。


Your code has a memory leak: 您的代码存在内存泄漏:

CAN_Signal* can_signal = new CAN_Signal(s, i);
if (can_signal->read_line() == false)
    return false;

If that condition is true, then the newly allocated CAN_Signal will be leaked since the pointer is neither deleted nor stored anywhere when the function returns. 如果该条件成立,则新分配的CAN_Signal将泄漏,因为函数返回时指针既不会删除也不存储在任何地方。


Your code is not exception safe: If any of these lines throws, then the pointer is leaked. 您的代码不是异常安全的:如果这些行中的任何一条抛出,则指针将泄漏。

if (can_signal->read_line() == false)
    return false;
can_signal->generate_data();
can_signals.push_back(can_signal);

It is unclear, why you would want to use explicit memory management in the first place. 目前尚不清楚,为什么首先要使用显式内存管理。 Unless there is a reason, I recommend that you don't do that and instead use std::vector <CAN_Signal> can_signals . 除非有原因,否则建议您不要这样做,而应使用std::vector <CAN_Signal> can_signals This would fix your problems with memory leaks and exception safety, remove the need to implement a custom destructor, and make the implementation of copy/move constructor/assignment of File_Output simpler. 这将解决内存泄漏和异常安全问题,消除实现自定义析构函数的需要,并使File_Output的复制/移动构造函数/赋值的实现File_Output简单。

Note that if you do that, you must reserve the memory for the elements for can_signals to prevent reallocation because the pointers in other vectors would be invalidated upon reallocation. 请注意,如果这样做,则必须为can_signals的元素reserve内存,以防止重新分配,因为其他向量中的指针将在重新分配时失效。 As a side-effect, this makes the program slightly faster. 副作用是,这会使程序速度稍快。

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

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