简体   繁体   中英

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. 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. 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 . 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.


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 . 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.

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. As a side-effect, this makes the program slightly faster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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