munkres-cpp icon indicating copy to clipboard operation
munkres-cpp copied to clipboard

Memory leak if bad_alloc happens

Open demin-dmitriy opened this issue 7 years ago • 0 comments

Just want to note, that whenever you write code (you usually see this kind of thing in various tutorials) like this

    m_matrix = new T*[rows]; // rows
    for ( size_t i = 0 ; i < rows ; i++ ) {
      m_matrix[i] = new T[columns]; // columns
    }

you have a memory leak, because any of the new T[columns] can throw an exception. Standard way to fix that is to either use std::unique_ptr or std::vector. But even better thing to do here is to use one std::vector of size columns * rows, because that will improve cache locality.

demin-dmitriy avatar Aug 04 '17 14:08 demin-dmitriy